Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

10 of 11
Make it more obvious that the query needs to be changed before it will work
David Wolever
  • 155.9k
  • 94
  • 366
  • 514

This will drop existing connections except for yours:

Query pg_stat_activity and get the pid values you want to kill, then issue SELECT pg_terminate_backend(pid int) to them.

PostgreSQL 9.2 and above:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB' ← change this to your DB
 AND pid <> pg_backend_pid();

PostgreSQL 9.1 and below:

SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB' ← change this to your DB
 AND procpid <> pg_backend_pid();

Once you disconnect everyone you will have to disconnect and issue the DROP DATABASE command from a connection from another database aka not the one your trying to drop.

Note the renaming of the procpid column to pid. See this mailing list thread.

Kuberchaun
  • 30.5k
  • 8
  • 54
  • 59

AltStyle によって変換されたページ (->オリジナル) /