排查 CentOS 上 PostgreSQL 报错,需要先定位具体错误信息。下面我给你一套通用排查思路 + 常见报错及解决方法,你可以对照使用;如果还不行,把完整报错贴出来我可以精准帮你分析。
请先执行以下命令,把结果贴出来:
# PostgreSQL 版本
psql --version
# 服务状态
systemctl status postgresql
# 监听端口
ss -lntp | grep 5432
Job for postgresql.service failed because the control process exited with error code.
journalctl -xe
journalctl -u postgresql
✅ 数据目录权限不对
chown -R postgres:postgres /var/lib/pgsql/15/data
chmod 700 /var/lib/pgsql/15/data
✅ 配置文件错误
sudo -u postgres /usr/pgsql-15/bin/postgres -D /var/lib/pgsql/15/data
could not connect to server: No such file or directory
systemctl start postgresql
pg_hba.confvim /var/lib/pgsql/15/data/pg_hba.conf
临时测试可加:
local all all trust
host all all 127.0.0.1/32 md5
然后:
systemctl restart postgresql
could not bind IPv4 address "0.0.0.0": Address already in use
ss -lntp | grep 5432
kill -9 PID
或修改端口:
vim postgresql.conf
port = 5433
initdb: error: could not create directory
mkdir -p /var/lib/pgsql/15/data
chown postgres:postgres /var/lib/pgsql/15/data
然后:
sudo -u postgres initdb -D /var/lib/pgsql/15/data
encoding "UTF8" does not match locale
sudo localectl set-locale LANG=en_US.UTF-8
或初始化时指定:
initdb -E UTF8 --locale=en_US.UTF-8
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
dnf install -y postgresql15-server
yum install postgresql-server
postgresql-setup initdb
systemctl start postgresql
postgresql.conf 是否配置监听pg_hba.conf 是否允许连接你可以直接贴出:
systemctl status postgresqljournalctl -u postgresql我可以一步一步帮你修 ✅