PostgreSQL: list and disconnect all open connections

20 12 2014

To list all currently open connections to your PostgreSQL database you can execute this SQL command:

SELECT * FROM pg_stat_get_activity(NULL::integer);

And if you need to manually close the connection that exists in the table above, you can execute the query:

SELECT pg_terminate_backend(
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '<YOUR_DB_NAME>'
AND pid <> pg_backend_pid();

Be careful – if your database client has some connection pool or is currently running a transaction – this will be interrupted, since the query ignores it.



One response

8 01 2015

Reblogged this on Eknaprasath.

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: