Kill PostgreSQL session

Sometimes we have to kill User sessions on PostgreSQL database server for various reasons. We can kill session with PostgreSQL function pg_terminate_backend(). To kill the session, we have to find out user sessions by running following query. If we know the user process id(pid), then execute following query.

SELECT pg_terminate_backend(41864) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'postgres';

Step1: Get the process ids of the user.

In this example, we want to find user anil’s session and kill it

postgres=> select datname,pid,usename from pg_stat_activity where usename='anil'; datname | pid | usename ----------+-------+--------- postgres | 41864 | anil (1 row)

step2: Kill the process id(PID) 41864 on posgres.

Query:

SELECT pg_terminate_backend(41864) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'postgres';

WARNING: PID 41864 is not a PostgreSQL server process
pg_terminate_backend
———————-
t
f
(2 rows)

Related Posts

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools
%d bloggers like this: