490

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)

2

5 Answers 5

877
+50

Oh, I just found that command on PostgreSQL forum:

SELECT * FROM pg_stat_activity;

To limit to just one database:

SELECT * FROM pg_stat_activity WHERE datname = 'dbname';
Sign up to request clarification or add additional context in comments.

6 Comments

If you would like to limit it to just one database, you can use SELECT * FROM pg_stat_activity WHERE datname = 'dbname';
How can i get the active database connection from the specific backed service?
And how about after running pg_terminate_backend and my app is still able to run query against the db but I could not see the new connections in pg_Stat_activity?
if you want to make the output more human readable, run \x on; first.
Short n' sweet & focused on remotes: SELECT datname,usename,application_name,client_addr FROM pg_stat_activity;
|
109

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.

3 Comments

Does idle means connection is active?. If I m releasing connection, still will it be listed as idle?
Yes @ShivamKubde but as 'idle', and the query above only show 'active' connections, so remove the WHERE ... clause and to be able to see what connections are active or idle add the column state to the SELECT clause
@ReneChan it is due to too many connections to your database. Normally the maximum is 100. You must have created more than that.
53
SELECT * FROM pg_stat_activity WHERE datname = 'dbname' and state = 'active';

Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active, database name and connection state should be included in the query to get the desired output.

Comments

18

You can check connection details in Postgres using pg_stat_activity. You can apply filter to satisfy your condition. Below are queries. References: https://orahow.com/check-active-connections-in-postgresql/

SELECT * FROM pg_stat_activity WHERE state = 'active';
select * from pg_stat_activity where state = 'active' and datname = 'REPLACE_DB_NAME_HERE';

Comments

18

If you would like to use PgAdmin (for me it is more than convenient), you could do these simple steps. Glad if this is helps

SELECT * FROM pg_stat_activity;

example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.