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

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

Actually, I require this kind of script during one of the maintenance tasks, in which we require to close all connections of a PostgreSQL Database.

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

You require this type of script very occasionally, but I am sharing here because this is also one of the important script 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
Brainfire Supplement
11 months 3 days ago

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