簡體   English   中英

如何獲取連接到PostgreSQL服務器的客戶端計算機名稱/ IP地址

[英]How to get Client Machine Name/ IP Address connecting to PostgreSQL Server

我們正在使用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;

SELECT inet_client_addr()沒有提供正確的IP地址。

在這方面需要幫助。

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_addrpg_stat_activity.client_hostname應該可以幫助您

client_addr

與此后端連接的客戶端的IP地址。 如果此字段為null,則表明客戶端是通過服務器計算機上的Unix套接字連接的,或者這是內部進程,例如autovacuum。

client_hostname

由client_addr的反向DNS查找報告的已連接客戶端的主機名。 僅對於IP連接,並且僅當啟用log_hostname時,此字段才為非空。

客戶端IP也可以位於HAproxy或pgbouncer或諸如此類的后面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM