postgres - list and terminate active connection to the postgresserver.
Sometimes we may needt to check for active connection and if they are connected, we would like to terminate cancel it.
There are the SQL command that might be able to assist.
SELECT
pid,
usename,
datname,
application_name,
client_addr,
backend_start,
state,
query_start,
query
FROM pg_stat_activity
ORDER BY query_start;
-- Kill a specific backend process by its PID
SELECT pg_terminate_backend(73);
-- Cancel a specific backend process by its PID
SELECT pg_cancel_backend(64)
-- Kill all other backend processes except the current one
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid();
Comments