Follow dba-ninja.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

dba-ninja.com Links

Dba_db2_button

Sqlserver_dba_button

How to kill a PostgreSQL user session connection

28 April,2022 by Rambler

Question: I'm executing a DROP DATABASE my_db and getting this message , how can I kill user session connections for this specific database ?

DETAIL: There is 1 other session using the database.

 

Answer:  This query will kill user session connections , except your connection to a specific PostgreSQL database

SELECT pg_terminate_backend(pid) FROM pg_stat_activity

WHERE

-- Not my connection

pid <> pg_backend_pid()

-- only kill connections to this specific database

AND datname = 'my_database_name' ;

 

You may need to also execute this REVOKE CONNECT command to stop other incoming connections

 

REVOKE CONNECT ON DATABASE my_database FROM PUBLIC, username;

 

 


Author: Rambler (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to kill a PostgreSQL user session connection


dba-ninja.com