如何解决PostgreSQL的postgres用户密码认证失败问题?
解决PostgreSQL密码认证失败(FATAL: password authentication failed for user "postgres")
确认配置修改已生效
修改pg_hba.conf后必须重启PostgreSQL服务,否则新配置不会加载:- macOS(EnterpriseDB安装):
brew services restart postgresql或通过EnterpriseDB自带的服务管理工具重启 - Windows:打开「服务」面板找到PostgreSQL服务,右键选择「重启」
- Linux:
systemctl restart postgresql
- macOS(EnterpriseDB安装):
重置postgres用户密码(最直接的解决方式)
如果确认密码输入错误或遗忘,按以下步骤重置:- 停止PostgreSQL服务
- 打开
pg_hba.conf,将localhost对应的IPv4和IPv6认证行改为trust:# IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust - 启动PostgreSQL服务
- 终端执行
psql -U postgres,无需密码直接登录 - 在psql中执行密码修改命令:
ALTER USER postgres PASSWORD '你的新密码'; - 将
pg_hba.conf改回md5认证 - 再次重启服务,使用新密码登录
检查IPv6连接的配置匹配
报错显示连接到localhost (::1)(IPv6地址),需确保pg_hba.conf中IPv6的本地连接行设置为md5,否则会出现认证失败。验证postgres用户状态
用trust方式登录后,执行\du查看用户列表,确认postgres用户存在且权限正常。
内容的提问来源于stack exchange,提问作者Mago223
相关产品推荐
相关产品推荐

