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

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type