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();