大网数据广告活动
服务器运维问答

How to find the ports of all running PostgreSQL instances (as a non-root user)

· 52CE 我爱测

I have a monitoring script (running as an unprivileged user) that needs to check all instances of Postgres currently running on the same host (Ubuntu Linux 18.04). Is there an easy way to find all their port numbers? I was thinki…

问题原文

原帖链接 · EM0 · CC BY-SA 4.0

I have a monitoring script (running as an unprivileged user) that needs to check all instances of Postgres currently running on the same host (Ubuntu Linux 18.04). Is there an easy way to find all their port numbers?

I was thinking of listing all files matching /var/run/postgresql/.s.PGSQL.* but I'm not sure whether this is reliable or the easiest way.

已采纳解答

原帖链接 · Daniel Vérité · CC BY-SA 4.0

An easy way with Debian/Ubuntu would be to use pg_lsclusters (as provided by the postgresql-common package). This command lists the clusters with ports, statuses and other information:

$ pg_lsclusters 
Ver Cluster Port Status Owner    Data directory              Log file
11  main    5433 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
12  main    5432 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

$ ports=$(pg_lsclusters --no-header | awk '{ print $3; }')

$ echo $ports
5433 5432

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。