问题原文
原帖链接 · 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
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
