You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1环境下PostgreSQL超级用户登录权限问题求助

解决Mac M1上PostgreSQL超级用户无法连接的问题

一、彻底清理重复安装的PostgreSQL痕迹

brew与EnterpriseDB的双重安装会导致配置冲突,需完全清除所有相关文件与服务:

  • 停止所有PostgreSQL进程:
    1. 查看brew运行的服务:brew services list,若存在postgresql@15,执行停止命令:brew services stop postgresql@15
    2. 查找并终止所有postgres相关进程:ps aux | grep postgres,找到进程ID后执行sudo kill -9 [进程ID]
  • 卸载所有PostgreSQL版本:
    1. brew卸载并清理残留:brew uninstall postgresql@15 && brew cleanup postgresql@15
    2. 卸载EnterpriseDB版本:运行/Library/PostgreSQL/15/uninstall-postgresql(若存在),或手动删除目录:sudo rm -rf /Library/PostgreSQL
  • 删除残留文件:
    sudo rm -rf /opt/homebrew/var/postgresql@15
    sudo rm -rf ~/.psqlrc ~/.psql_history
    
  • 暂时注释掉.zshrc中的Postgres配置(后续重新配置)

二、重新安装并初始化brew版PostgreSQL@15

  1. 更新brew到最新版本:brew update
  2. 安装PostgreSQL@15:brew install postgresql@15
  3. 初始化数据库(适配M1设备的brew路径):
    initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@15
    
  4. 启动PostgreSQL服务:brew services start postgresql@15

三、创建/恢复超级用户cris并连接

  1. 先以默认超级用户postgres连接数据库:psql postgres
  2. 在psql终端内创建超级用户cris(替换为你的自定义密码):
    CREATE USER cris WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'your-password';
    
  3. 退出psql终端:\q
  4. 直接使用cris超级用户连接:psql -U cris postgres

四、修正配置与目录权限

  1. 编辑.zshrc,确保Postgres配置正确(优先brew的bin路径):
    # Postgres
    export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"
    export PGDATA="/opt/homebrew/var/postgresql@15"
    
  2. 使配置立即生效:source ~/.zshrc
  3. 确保数据目录权限归属当前系统用户:
    sudo chown -R cris:staff /opt/homebrew/var/postgresql@15
    

完成以上步骤后,即可正常以超级用户cris身份连接PostgreSQL。

内容的提问来源于stack exchange,提问作者Cristian Muscalu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:46:01