Skip to content

Instantly share code, notes, and snippets.

@luiscape
Created July 26, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luiscape/ee7aeb49aea96ebe7dd11339cb29e16a to your computer and use it in GitHub Desktop.
Save luiscape/ee7aeb49aea96ebe7dd11339cb29e16a to your computer and use it in GitHub Desktop.
Closes all PostgreSQL connections except for the one sending this query.
#
# The following statement closes
# all PostgreSQL connections except
# for the one currently open (i.e.
# the one sending the query).
#
# Original solution by: http://goo.gl/znBpH
#
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND pid <> pg_backend_pid();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment