在 Ubuntu 上使用 pgAdmin 连接 PostgreSQL 失败,常见原因和排查方法如下(按出现频率排序):
现象:连接超时或 could not connect to server
检查服务状态:
sudo systemctl status postgresql
启动服务:
sudo systemctl start postgresql
sudo systemctl enable postgresql
现象:pgAdmin 在本机可以连,远程连不上
编辑配置文件:
sudo nano /etc/postgresql/*/main/postgresql.conf
确认:
listen_addresses = 'localhost' # 或 '*'
如需远程连接:
listen_addresses = '*'
重启:
sudo systemctl restart postgresql
现象:no pg_hba.conf entry
编辑:
sudo nano /etc/postgresql/*/main/pg_hba.conf
示例(允许本地密码连接):
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
远程示例:
host all all 0.0.0.0/0 md5
修改后:
sudo systemctl reload postgresql
pgAdmin 中常见错误:
localhost 或 127.0.0.15432postgres测试端口:
telnet localhost 5432
# 或
nc -zv localhost 5432
现象:password authentication failed
设置密码:
sudo -u postgres psql
ALTER USER postgres WITH PASSWORD 'yourpassword';
Ubuntu 上常见:
建议:
sudo snap remove pgadmin4
sudo apt install pgadmin4
或官网 .deb 安装
sudo ufw allow 5432
| 错误信息 | 原因 |
|---|---|
| connection refused | 服务未启动 / 端口错 |
| timeout | 防火墙 / 监听地址 |
| no pg_hba.conf entry | 权限配置 |
| password failed | 密码错 |
如果你愿意,可以告诉我:
我可以帮你精准定位。