How to select list of active connections to a PostgreSQL database

断了今生、忘了曾经 提交于 2019-12-20 08:22:07

问题


Is there a command in PostgreSQL to select active connections to a given database?

psql states that I can't drop one of my databases because there are active connections to it, so I would like to see what the connections are (and from which machines)


回答1:


Oh, I just found that command on PostgreSQL forum:

SELECT * FROM pg_stat_activity;



回答2:


Following will give you active connections/ queries in postgres DB-

SELECT 
    pid
    ,datname
    ,usename
    ,application_name
    ,client_hostname
    ,client_port
    ,backend_start
    ,query_start
    ,query
    ,state
FROM pg_stat_activity
WHERE state = 'active'

You may use 'idle' instead of active to get already executed connections/queries.



来源:https://stackoverflow.com/questions/27435839/how-to-select-list-of-active-connections-to-a-postgresql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!