PostgreSQL: Script to Stop all Connections and Force to Drop the Database

In this post, I am providing a script to stop all running connections of a database and force to drop the database.

In our organization, I have created one database for production report only. This database works as a backup and report server and only one or two services using this database server.
I have created this server for temporary purposes and now I require to drop one the report database, but I was facing problem for running connections.

I know that running connections are not that much important for me because this setup is only for our internal data report purpose.

I have worked around this and find script to stop all connections of a Postgres database and drop that Postgres database.

As a Database Administrator, we are also responsible to manage and remove this kind of unwanted objects or databases.

These are the steps to perform an action:

First, Make sure that no one can connect to this database:

Second, execute this script to terminate all connections to a database
(For postgres < 9.2):

Drop now:

Note: Step one and two require super user permission and only database owner can drop this 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 Stop all Connections and Force to Drop the Database"

Notify of

Sort by:   newest | oldest | most voted
10 months 7 days ago

Woah! I’m reallly enjoying the template/theme of this website.
It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” betwween usability and appearance.
I must saay you’ve done a amazing job with this. In addition, the blog loads extremely fast
for mme on Safari. Outstanding Blog!