PostgreSQL - How to drop database with active connections?
Drop database with active connection from another user in PostgreSQL
You need to terminate all the active connections first:
SELECT
pg_terminate_backend(pg_stat_activity.pid)
FROM
pg_stat_activity
WHERE
pg_stat_activity.datname = 'database_name'
AND pid <> pg_backend_pid();
or you can also use this query:
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_get_activity(NULL::integer)
WHERE
datid = (
SELECT
oid
FROM
pg_database
WHERE
datname = 'database_name');
Then you can drop the database:
DROP DATABASE database_name;
Need a good GUI tool for PostgreSQL? Check out TablePlus. It’s native, beautiful, and available for free.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.