温馨提示×

CentOS如何查看PostgreSQL状态

小樊
34
2026-08-20 03:40:21
栏目: 云计算

CentOS 上查看 PostgreSQL 的状态,常用方法取决于你是使用 systemd 还是直接通过 PostgreSQL 自带的工具。下面按常见场景说明。


一、使用 systemd(最常见,CentOS 7/8/Stream)

1️⃣ 查看 PostgreSQL 服务状态

systemctl status postgresql

或(如果安装了多个版本,例如 PostgreSQL 14):

systemctl status postgresql-14

✅ 成功状态示例:

Active: active (running) since ...

❌ 未运行示例:

Active: inactive (dead)

2️⃣ 查看是否开机自启

systemctl is-enabled postgresql

3️⃣ 启动 / 停止 / 重启

sudo systemctl start postgresql
sudo systemctl stop postgresql
sudo systemctl restart postgresql

二、使用 PostgreSQL 自带命令(推荐)

1️⃣ 切换到 postgres 用户

sudo -i -u postgres

2️⃣ 查看数据库运行状态

pg_isready

✅ 正常输出示例:

/var/run/postgresql:5432 - accepting connections

❌ 异常示例:

no response

三、进入数据库查看运行状态(高级)

1️⃣ 进入 psql

psql

2️⃣ 查看数据库列表

\l

3️⃣ 查看当前连接

SELECT * FROM pg_stat_activity;

四、查看 PostgreSQL 进程

ps aux | grep postgres

或:

pgrep -a postgres

五、查看监听端口(确认是否运行)

ss -lntp | grep postgres

或:

netstat -lntp | grep postgres

默认端口:

5432

六、查看日志(排查问题)

日志位置通常在:

/var/lib/pgsql/data/log/

或:

/var/log/postgresql/

查看最新日志:

tail -f /var/lib/pgsql/data/log/postgresql-*.log

✅ 总结(最常用)

systemctl status postgresql
pg_isready
ps aux | grep postgres

如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • PostgreSQL 安装方式(yum / 编译 / Docker)

我可以给你更精确的命令。

0