Terminating all active connection and dropping database — PostgreSQL

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/

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store