Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created June 18, 2018 18:10
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jeffjohnson9046/89533eb8d2be5cfe0b1a46d9e83ada5d to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/89533eb8d2be5cfe0b1a46d9e83ada5d to your computer and use it in GitHub Desktop.
How to kill all connections to a Postgres database
-- Accepted answer from here: https://stackoverflow.com/questions/5408156/how-to-drop-a-postgresql-database-if-there-are-active-connections-to-it
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '[your database name goes here]'
AND pid <> pg_backend_pid();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment