PG: Drop DB Failed

Error

ERROR:  database "staging_db" is being accessed by other users
DETAIL:  There is 1 other session using the database.

Solution

Prevent future connections

REVOKE CONNECT ON DATABASE thedb FROM public;

Terminate all connections

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

Reference:

Postgresql-unable-to-drop-database-because-of-some-auto-connections-to-db