PostgreSQL 连接失败的定位与修复
客户端报 could not connect to server、FATAL: password authentication failed、no pg_hba.conf entry、too many connections 或 SSL error。
pg_isready -h <host> -p 5432
ss -lntp | grep 5432
psql -h <host> -p 5432 -U <user> -d <db> -c 'SELECT 1'
psql -c 'SHOW max_connections'
psql -c "SELECT state, count(*) FROM pg_stat_activity GROUP BY state"
psql -c 'SHOW ssl'
服务端:
journalctl -u postgresql --since '30 min ago'
tail -100 /var/log/postgresql/postgresql-*.log
listen_addresses 只监听本地。pg_hba.conf 没有匹配的客户端地址或认证方式。max_connections,连接池泄漏。sslmode=require 但服务端未启用 TLS。pg_isready 和 ss 确认服务与监听地址。listen_addresses 和 pg_hba.conf,reload 后重试。max_connections,同时优化连接池和空闲连接。ssl 配置和客户端 sslmode,统一证书。psql — 连接与查询pg_isready — 服务可用性pg_ctl — 服务控制journalctl — 服务端日志