luiscape
7/26/2016 - 7:38 PM

Closes all PostgreSQL connections except for the one sending this query.

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