Useful shortcuts for vi editor

Friday, 18 May 2018

"database "example" is being accessed by other users" solution

Action
postgres=# drop database example;

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

Solution
Run following sql code;

postgres=# 
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'example' AND pid <> pg_backend_pid();

All sessions should be terminated where belong to "example" database except backend sessions.

Lastly, try to drop it again.