Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Pg_Terminate_Backend() To Kill A Connection In Postgresql

You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.

SELECT
   pg_terminate_backend(pid)
FROM
   pg_stat_activity
WHERE
   -- don't kill my own connection!
   pid <> pg_backend_pid()
   -- don't kill the connections to other databases
   AND datname = 'database_name'
   ;

Before executing this query, you have to REVOKE the CONNECT privileges to avoid new connections:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;

If you're using Postgres 8.4-9.1 use procpid instead of pid
SELECT
   pg_terminate_backend(procpid)
FROM
   pg_stat_activity
WHERE
   -- don't kill my own connection!
   procpid <> pg_backend_pid()
   -- don't kill the connections to other databases
   AND datname = 'database_name'
   ;



Comments

Popular posts from this blog

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

7 Steps to configure BDR replication in postgresql

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

Ora2PG - Oracle/MySQL to Postgres DB migration Version 20.0

PostgreSQL Introduction