在 CentOS 上 pgAdmin 连不上 PostgreSQL,常见原因基本集中在 服务、网络、认证、防火墙、配置 这几类。下面按“最常见 → 较少见”的顺序给你排查清单。
systemctl status postgresql
或(老版本)
service postgresql status
如果没启动:
systemctl start postgresql
systemctl enable postgresql
ss -lntp | grep 5432
正常应看到:
LISTEN 0 128 0.0.0.0:5432
如果只有:
127.0.0.1:5432
那只能本机连。
编辑:
vi /var/lib/pgsql/data/postgresql.conf
(版本不同路径可能不同)
listen_addresses = '*'
然后重启:
systemctl restart postgresql
vi /var/lib/pgsql/data/pg_hba.conf
如果是本地 pgAdmin(同一台机器):
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
如果 pgAdmin 在 另一台机器:
host all all 0.0.0.0/0 md5
(生产环境建议限制 IP)
修改后:
systemctl reload postgresql
firewall-cmd --list-all
如果没有 5432:
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
云服务器还要看 安全组。
| 项目 | 正确示例 |
|---|---|
| Host | localhost 或服务器 IP |
| Port | 5432 |
| Username | postgres |
| Password | 安装时设置的 |
| Maintenance DB | postgres |
⚠️ CentOS 上 PostgreSQL 默认用户是 postgres,不是 root。
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
如果这样能连,说明是 SELinux 策略问题。
确认:
pgadmin4 --version
systemctl status postgresql
ss -lntp | grep 5432
firewall-cmd --list-all
psql -U postgres -h 127.0.0.1
你可以把下面信息发我:
ss -lntp | grep 5432 输出pg_hba.conf 相关行我可以直接告诉你是哪一步卡住。