Terminating all active connection and dropping database — PostgreSQL

Bharat Dwarkani
1 min readMay 7, 2020

--

While using PostgreSQL some time you may come across a case to drop whole database. If that database is actively being used you may get an error that cannot drop database due to active connections. Following 2 steps specified below database can be dropped.

Drop all active connections

Connect to server using admin login and switch to default DB postgre. Run this script to drop all active connections

SELECT  pg_terminate_backend (pg_stat_activity.pid) FROM  pg_stat_activity WHERE  pg_stat_activity.datname = 'DatabaseName';

Drop Database

DROP DATABASE "DatabaseName";

For more such interesting technology related blogs, resources visit my site https://sharetechlinks.com/

--

--

Bharat Dwarkani
Bharat Dwarkani

Written by Bharat Dwarkani

Tech Enthusiast, Full Stack Software Engineer, Product Manager, Engineering Enterprise SaaS product

No responses yet