Finding and Killing Sessions in PostgreSQL

To kill a session in PostgreSQL, the pid for the session is needed. To get the pid, the following query can be run to get the active PostgreSQL sessions:

SELECT pid, datid, datname, usesysid, usename , query, client_addr, client_port, query_start, backend_start, wait_event, xact_start, state FROM pg_catalog.pg_stat_activity

The query results from the above query can be examined to determine which session to kill. Once the session to kill has been found, get the value of the pid column from the above query results. This pid value can then be passed to the pg_terminate_backend command to terminate the session. Below is an example:

SELECT pg_terminate_backend(8428);