Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Created May 6, 2024 08:13
Show Gist options
  • Save rehmatworks/e7765039fd043c40209cff86265440d1 to your computer and use it in GitHub Desktop.
Save rehmatworks/e7765039fd043c40209cff86265440d1 to your computer and use it in GitHub Desktop.
Drop a Postgres Database Forcefully
-- Set the database to reject new connections
ALTER DATABASE db_name CONNECTION LIMIT 0;
-- Terminate all existing connections to the database
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'db_name'
AND pid <> pg_backend_pid();
-- Drop the database
DROP DATABASE db_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment