在 CentOS 下查看 PostgreSQL 状态,常用方法如下(按使用频率排序):
适用于 CentOS 7 / 8 / Stream,PostgreSQL 以 systemd 服务方式运行。
systemctl status postgresql
如果是多版本或自定义服务名,可能是:
systemctl status postgresql-14
systemctl status postgresql-15
✅ 状态说明:
active (running):正在运行inactive (dead):未运行failed:启动失败systemctl is-enabled postgresql
ps -ef | grep postgres
常见进程示例:
/usr/pgsql-14/bin/postgres -D /var/lib/pgsql/14/data
✅ 能看到进程说明 PostgreSQL 正在运行。
pg_isready 是 PostgreSQL 自带的工具,用于检查数据库是否可接受连接。
pg_isready
输出示例:
/var/run/postgresql:5432 - accepting connections
常见返回:
accepting connections ✅ 正常no response ❌ 未启动或端口不通ss -lntp | grep 5432
或
netstat -lntp | grep 5432
默认端口是 5432。
su - postgres
psql
进入后执行:
SELECT version();
✅ 能正常执行说明数据库运行正常。
日志位置通常在:
/var/lib/pgsql/14/data/log/
或查看配置文件:
postgresql.conf
# 启动
systemctl start postgresql
# 停止
systemctl stop postgresql
# 重启
systemctl restart postgresql
systemctl status postgresql
pg_isready
如果你愿意,可以告诉我:
我可以给你更精确的命令。