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

标签 windows postgresql

我们正在使用 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_addrpg_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/

相关文章:

windows - 在 Windows 操作系统中打开文件时将默认程序重定向到另一个程序

SQL根据多个字段删除重复行

excel - Postgres 小数精度 VS Excel

postgresql - PostgreSQL 用于查询处理和优化的中间语言是什么?

c - 如何通过 C 在后台执行进程并使用 Windows API?

windows - FaSTLane windows 无法安装

database - 如果 postgres 的性能低下,我应该选择哪个数据库

postgresql - 使用 Cloud Shell 和 SSL 连接到 Google Cloud SQL PostgreSQL?

c++ - 获取 FILE * 句柄而不实际在磁盘上创建文件

c++ - 将 HTTP 连接绑定(bind)到特定适配器(提示 : Metered Connection)