CentOS 连接不上 PostgreSQL,常见原因和排查思路如下(从网络 → 服务 → 配置 → 权限逐步排查):
systemctl status postgresql
或
ps -ef | grep postgres
未启动:
systemctl start postgresql
systemctl enable postgresql
PostgreSQL 默认只监听 127.0.0.1
查看配置文件(路径可能不同):
# 常见路径
/var/lib/pgsql/data/postgresql.conf
/var/lib/pgsql/<版本>/data/postgresql.conf
检查:
listen_addresses = 'localhost'
改为(允许远程):
listen_addresses = '*'
重启:
systemctl restart postgresql
firewall-cmd --list-all
放行 5432:
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
临时测试:
setenforce 0
永久(谨慎):
vi /etc/selinux/config
SELINUX=disabled
编辑:
/var/lib/pgsql/data/pg_hba.conf
示例(允许所有 IP 密码连接,测试用):
host all all 0.0.0.0/0 md5
修改后:
systemctl reload postgresql
netstat -tunlp | grep 5432
# 或
ss -tunlp | grep 5432
应看到:
0.0.0.0:5432
本地:
psql -U postgres -h 127.0.0.1
远程:
psql -h <服务器IP> -U 用户名 -d 数据库名
| 报错 | 原因 |
|---|---|
| connection refused | 服务未启动 / 防火墙 |
| no pg_hba.conf entry | 未授权 IP |
| timeout | 网络 / 防火墙 |
| password authentication failed | 密码或用户错误 |
✅ 服务已启动
✅ listen_addresses=‘*’
✅ 防火墙放行 5432
✅ pg_hba.conf 已配置
✅ 使用正确 IP / 用户 / 密码
如果你能贴出:
psql 命令我可以直接帮你定位到具体原因。