Grimoire-
Command
.es

GNU+Linux command memo

Kill Postgresql sessions / connections

Fermer les connexions à une base de données PostgreSQL pour pouvoir la supprimer.

$ sudo -u postgres dropdb db_dev
dropdb: erreur : la suppression de la base de données a échoué : ERROR:  database "db_dev" is being accessed by other users
DÉTAIL : There are 4 other sessions using the database.
$ ./manage.py dbshell (1)
psql (17.11 (Debian 17.11-0+deb13u1))
Connexion SSL (protocole : TLSv1.3, chiffrement : TLS_AES_256_GCM_SHA384, compression : désactivé, ALPN : postgresql)
Saisissez « help » pour l'aide.

db_dev=> 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 = 'db_dev' (2)
    ;
 pg_terminate_backend
----------------------
 t
 t
 t
 t
(4 lignes)

db_dev=>
\q
1 Using Django to reach PostgreSQL shell
2 Don’t forget to put the name of your database instead of db_dev

Discovered here.