Terminating all active connection and dropping database - PostgreSQL

Bharat Dwarkani | May 7, 2020 | Database

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 PostgreSQL. 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";
visibility21 share bookmarks