我们正在使用 PostgreSQL 服务器。我们需要一个查询来获取所有客户端机器名称,IP 地址连接到该数据库。
找到以下查询。但它只返回最大连接数、可用连接数。但是我们需要客户端机器名称。
select max_conn, used, res_for_super, max_conn - used - res_for_super res_for_normal
from (
select count(*) used from pg_stat_activity
) t1,
(select setting::int res_for_super
from pg_settings
where name=$$superuser_reserved_connections$$
) t2,
(select setting::int max_conn
from pg_settings
where name=$$max_connections$$) t3;
SELECT inet_client_addr()
未提供正确的 IP 地址。
在这方面需要帮助。
最佳答案
https://www.postgresql.org/docs/current/static/functions-info.html
inet_client_addr
address of the remote connection
这是您的 session IP。
https://www.postgresql.org/docs/current/static/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW
pg_stat_activity.client_addr
和 pg_stat_activity.client_hostname
应该对您有帮助
client_addr
IP address of the client connected to this backend. If this field is null, it indicates either that the client is connected via a Unix socket on the server machine or that this is an internal process such as autovacuum.
client_hostname
Host name of the connected client, as reported by a reverse DNS lookup of client_addr. This field will only be non-null for IP connections, and only when log_hostname is enabled.
客户端 IP 也可以在 HAproxy 或 pgbouncer 或类似...的后面
关于windows - 如何获取连接到 PostgreSQL 服务器的客户端机器名称/IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48619772/