Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created May 23, 2017 11:52
Show Gist options
  • Save chanmix51/995f7576c88d541b16f1b9d2b2515aa5 to your computer and use it in GitHub Desktop.
Save chanmix51/995f7576c88d541b16f1b9d2b2515aa5 to your computer and use it in GitHub Desktop.
kill connections on a specific database
-- gist from http://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection
-- prevent people from connecting to the database
revoke connect on database :db_name from public, … ;
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = ':db_name'
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment