Force drop db while others may be connected

PostgreSQL 13 added:

DROP DATABASE mydb WITH (FORCE);

The manual:

FORCE

Attempt to terminate all existing connections to the target database. It doesn't terminate if prepared transactions, active logical replication slots or subscriptions are present in the target database.

This will fail if the current user has no permissions to terminate other connections. Required permissions are the same as with pg_terminate_backend, described in Section 9.27.2. This will also fail if we are not able to terminate connections.


In PostgreSQL 12 and earlier, you cannot drop a database while clients are connected to it.

At least, not with the dropdb utility - which is only a simple wrapper around DROP DATABASE server query.

Quite robust workaround follows:

Connect to your server as superuser, using psql or other client. Do not use the database you want to drop.

psql -h localhost postgres postgres

Now using plain database client you can force drop database using three simple steps:

  1. Make sure no one can connect to this database. You can use one of following methods (the second seems safer, but does not prevent connections from superusers).

    /* Method 1: update system catalog */
    UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'mydb';
    
    /* Method 2: use ALTER DATABASE. Superusers still can connect!
    ALTER DATABASE mydb CONNECTION LIMIT 0; */
    
  2. Force disconnection of all clients connected to this database, using pg_terminate_backend.

     SELECT pg_terminate_backend(pid)
     FROM pg_stat_activity
     WHERE datname = 'mydb';
    
     /* For old versions of PostgreSQL (up to 9.1), change pid to procpid:
    
     SELECT pg_terminate_backend(procpid)
     FROM pg_stat_activity
     WHERE datname = 'mydb'; */
    
  3. Drop the database.

     DROP DATABASE mydb;
    

Step 1 requires superuser privileges for the 1st method, and database owner privileges for the 2nd one. Step 2 requires superuser privileges. Step 3 requires database owner privilege.


Using @filiprem's answer in a my case and simplifying it:

-- Connecting to the current user localhost's postgres instance
psql

-- Making sure the database exists
SELECT * from pg_database where datname = 'my_database_name'

-- Disallow new connections
UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'my_database_name';
ALTER DATABASE my_database_name CONNECTION LIMIT 1;

-- Terminate existing connections
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'my_database_name';

-- Drop database
DROP DATABASE my_database_name

Postgres 13 adds the FORCE option for DROP DATABASE. See filiprem's answer.

The shell utility dropdb is basically just a wrapper around the SQL command and inherits the same option. So it's simple and reliable from the shell now, too:

dropdb mydb --force

Or short:

dropdb mydb -f

For Postgres 12 and older, there is a way with the shell utilities dropdb & pg_ctl (or pg_ctlcluster in Debian and derivates), too. But filiprem's method is superior there for several reasons:

  • It only disconnects users from the database in question.
  • It does not need to restart the whole cluster.
  • It prevents immediate reconnects, possibly spoiling the dropdb command.

I quote man pg_ctlcluster:

With the --force option the "fast" mode is used which rolls back all active transactions, disconnects clients immediately and thus shuts down cleanly. If that does not work, shutdown is attempted again in "immediate" mode, which can leave the cluster in an inconsistent state and thus will lead to a recovery run at the next start. If this still does not help, the postmaster process is killed. Exits with 0 on success, with 2 if the server is not running, and with 1 on other failure conditions. This mode should only be used when the machine is about to be shut down.

pg_ctlcluster 9.1 main restart --force

or

pg_ctl restart -D datadir -m fast

or

pg_ctl restart -D datadir -m immediate

immediately followed by:

dropdb mydb

Possibly in a script for immediate succession.