PostgreSQL – Kill user session

Identify the session that you would like to terminate from the below view and copy the pid.

SELECT * FROM pg_stat_activity;

Kill the session as below where pid is the process id of the user session that you would like to terminate.

SELECT pg_terminate_backend(pid);

Use the below command to kill all the non-backend user sessions connected to a database.

SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.datname = 'your_database_name' AND pid <> pg_backend_pid();
See also  PostgreSQL - FROM Clause