在 Ubuntu 上使用 pgAdmin 连接 PostgreSQL 失败,常见原因和排查方法如下:
sudo systemctl status postgresql
如果没启动:
sudo systemctl start postgresql
sudo systemctl enable postgresql
PostgreSQL 默认只监听 localhost
sudo nano /etc/postgresql/<版本>/main/postgresql.conf
确认:
listen_addresses = 'localhost'
# 或
listen_addresses = '*'
改完后重启:
sudo systemctl restart postgresql
sudo nano /etc/postgresql/<版本>/main/pg_hba.conf
示例(允许本地和局域网):
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
修改后重启 PostgreSQL。
pgAdmin 中:
127.0.0.1 或 localhost5432postgres重置密码:
sudo -u postgres psql
ALTER USER postgres PASSWORD '新密码';
sudo ufw status
如果需要远程:
sudo ufw allow 5432
Prefer 或 Disable如果你用的是:
snap connect pgadmin4:network
| 错误 | 原因 |
|---|---|
| connection refused | 服务未启动 / 端口错 |
| no pg_hba.conf entry | 未授权 IP |
| password authentication failed | 密码错误 |
| timeout | 防火墙 / 网络 |
如果你愿意,可以把 具体报错信息 或 pgAdmin 配置截图 发出来,我可以精准帮你定位。