Skip to content

Instantly share code, notes, and snippets.

@iezg
Last active January 3, 2016 05:29
Show Gist options
  • Save iezg/8416352 to your computer and use it in GitHub Desktop.
Save iezg/8416352 to your computer and use it in GitHub Desktop.
Get Postgres active connections, and drop the idle ones
-- PostgreSQL 9.1 and below:
SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND procpid <> pg_backend_pid();
-- PostgreSQL 9.2 and above:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND pid <> pg_backend_pid();
-- get connections to current db
SELECT datname, usename, procpid, client_addr, waiting, query_start, current_query
FROM pg_stat_activity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment