PostgreSQL: Script to Kill all Running Connections and Sessions of a Database

In this post, I am sharing one script to kill all running connections and sessions of a PostgreSQL Database.

Actually, I need this script during PostgreSQL maintenance task, in which we require to close all connections and sessions.

Before executing this script, please take care and verify all running connections and processes otherwise this script will harm to your data.

You may require this type of script very occasionally, but I am sharing here because this is also one of the important scripts for PostgreSQL DBA.

This script will work after PostgreSQL 9.1.

Script to kill all running connections by specifying a database name:

Script to kill all running connections of a current database:

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

1 Comment on "PostgreSQL: Script to Kill all Running Connections and Sessions of a Database"

Notify of
Sort by:   newest | oldest | most voted
Brainfire Supplement

Way cool! Some extremely valid points! I appreciate you
penning this write-up plus the rest of the website is really good.