如何获取连接到 PostgreSQL 服务器的客户端机器名称/IP 地址

Sri*_*olu 1 windows postgresql

我们正在使用 PostgreSQL 服务器。我们需要一个查询来获取所有客户端机器名称,IPAdresses 连接到该数据库。

发现下面的查询。但它只返回最大连接数,可用连接数。但是我们需要客户端机器名称。

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;
Run Code Online (Sandbox Code Playgroud)

SELECT inet_client_addr() 没有给出正确的 IP 地址。

在这方面需要帮助。

Vao*_*sun 6

https://www.postgresql.org/docs/current/static/functions-info.html

inet_client_addr 远程连接地址

这是您的会话 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应该帮助你

客户端地址

连接到此后端的客户端的 IP 地址。如果此字段为空,则表示客户端通过服务器计算机上的 Unix 套接字连接,或者这是内部进程,例如 autovacuum。

客户端主机名

已连接客户端的主机名,由 client_addr 的反向 DNS 查找报告。对于 IP 连接,并且仅当启用 log_hostname 时,此字段才会为非空。

客户端 IP 也可以在 HAproxy 或 pgbouncer 或类似...