psql kill all sessions code example
Example 1: view and kill postgresql connections to database
SELECT * FROM pg_stat_activity;
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid != pg_backend_pid()
AND datname = 'clinical_data';
select pg_terminate_backend(13337)
Example 2: postgres killing connections on db
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
AND datname = 'database_name'
;
Example 3: postgres kill all connections
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
AND datname = 'database_name';