Last active
April 3, 2019 20:59
-
-
Save invalidusrname/4668759 to your computer and use it in GitHub Desktop.
Kills active connections to a postgres database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 9.1 | |
-- Terminate connections to a database | |
SELECT | |
pg_terminate_backend(procpid) | |
FROM | |
pg_stat_activity | |
WHERE | |
-- don't kill my own connection! | |
procpid <> pg_backend_pid() | |
-- don't kill the connections to other databases | |
AND query_start < (NOW() - '1 month'::INTERVAL) and application_name = '' | |
-- 9.2 | |
-- Terminate connections to a database | |
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 query_start < (NOW() - '1 month'::INTERVAL) and application_name = '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SELECT
datid, datname, pid, usename, application_name, client_addr, backend_start, query_start
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
ORDER BY datname, application_name, backend_start