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

通过pgAdmin连接PostgreSQL失败:postgres用户密码验证错误

PostgreSQL与pgAdmin4本地连接密码验证失败问题解决(Ubuntu 22.04 LTS)

问题概况

在Ubuntu 22.04 LTS环境下配置PostgreSQL与pgAdmin4 7.3,已完成以下操作:

  • 安装并启动PostgreSQL:
    sudo apt update
    sudo apt install postgresql postgresql-contrib
    
    sudo service postgresql start
    
  • 通过sudo passwd postgres设置系统postgres用户密码
  • 完成pgAdmin4 7.3版本安装

连接服务器时触发以下错误:

unable to connect to server: connection failed: fatal: password authentication failed for user "postgres" connection to server at "localhost" (127.0.0.1). port 5432 failed: fatal: password authentication failed for user "postgres"

已确认的配置信息:

  • pg_hba.conf配置无异常
  • postgresql.conf关键参数:
    listen_addresses = 'localhost'
    
    port = 5432
    
    password_encryption = scram-sha-256
    

解决步骤

1. 重置PostgreSQL内部postgres用户密码

sudo passwd postgres仅修改了Linux系统层面postgres用户的登录密码,PostgreSQL数据库内部的postgres用户密码需通过SQL命令单独设置:

# 切换至系统postgres用户并进入PostgreSQL命令行
sudo -u postgres psql
# 设置适配SCRAM认证的数据库用户密码
ALTER USER postgres WITH PASSWORD '你的自定义密码';
# 退出PostgreSQL命令行
\q

2. 确认pg_hba.conf的认证规则

检查pg_hba.conf(通常位于/etc/postgresql/<版本号>/main/目录下)中本地连接的认证方式,确保与password_encryption配置匹配:
找到对应IPv4和IPv6本地连接的配置行,修改为:

# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256

修改完成后重启PostgreSQL服务:

sudo service postgresql restart

3. 核对pgAdmin4连接参数

在pgAdmin4创建服务器连接时,确保以下参数正确:

  • 主机名/地址:localhost
  • 端口:5432
  • 维护数据库:postgres
  • 用户名:postgres
  • 密码:填写步骤1中设置的PostgreSQL内部用户密码

4. 清除连接缓存

若之前多次尝试错误密码,pgAdmin4可能缓存了旧凭据:

  • 在连接编辑界面重新输入密码并保存
  • 关闭pgAdmin4后重启,若为Web版则清除浏览器缓存

内容的提问来源于stack exchange,提问作者Adi Akhilesh Singh 5-Year IDD

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 05:43:26