Live to Code

code to live

Terminate postgres connections to a database


19 Sep 2012 |

Sometimes I want to alter a PostgreSQL database in some way but I am prevented by other connections, as this error indicates:

ERROR:  database "my_database" is being accessed by other users
DETAIL:  There are 2 other session(s) using the database.

It's not always easy to figure out which applications have open sessions, so here's a great script I found on Stack Overflow to terminate all connections to the database:

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 = 'my_database'
    ;