Kill the session for particular database/user in PostgreSQL

Kill the session for particular User in PostgreSQL

Check all the connected session in PostgreSQL:

select * from pg_stat_activity; 

Kill the Session for particular database in PostgreSQL

    pid <> pg_backend_pid()
    AND datname = 'database_name';

Note: pg_backend_pid() function return the current session pid running which used in above query to avoid kill the same session or current session which running the query.

Kill the session for particular User in PostgreSQL:

select pg_terminate_backend(pid)
from pg_stat_activity
where usename = 'HR' AND datname = 'database_name';

Kill idle Session of particular database in PostgreSQL:

select pid, application_name, state, query from pg_stat_activity where state = 'IDLE' AND datname = 'database_name';

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.