Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Kill all Running Connections and Sessions of a Database

I am sharing a script to kill all running connections and sessions of a PostgreSQL Database.

I need this script during the PostgreSQL maintenance task, in which we require to close all connections and sessions.

Before executing this script, please take care and verify all running connections and processes otherwise this script will harm to your data or transactions.

You may require this type of script very occasionally, but I am sharing because this is also one of the necessary scripts for PostgreSQL DBA.

This script will work after PostgreSQL 9.1.
Script to kill all running connections by specifying a database name:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = 'datbase_name'
  AND pid <> pg_backend_pid();
Script to kill all running connections of a current database:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database()
  AND pid <> pg_backend_pid();

Comments

Popular posts from this blog

PostgreSQL pgBadger

ORA-01261: Parameter db_recovery_file_dest destination string cannot be translated ORA-01262: Stat failed on a file destination directory Linux-x86_64 Error: 2: No such file or directory

Migrating From Oracle to PostgreSQL using ora2pg open source tools

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

How to configure Replication Manager (repmgr) ?