Dropping All Postgres Connections

27 Apr 2017

For Postgres 9.2 and above, here’s how you can drop all active connections to your database. This is useful when you’re trying to drop a table, but someone’s still connected to it.

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database() AND pid <> pg_backend_pid();