PostgreSQL – find and kill open sessions

This select helps you find all the activity on the database.

select * from pg_stat_activity;

Use this select to kill all open sessions (except from the background own, the current session) for a specific database name.

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname like '%database%';

Leave a Reply