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
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
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';