Force Deleting a Postgres Database

pgAdmin doesn't allow deleting a database when there are existing connections present to the database and results in below error.

An error has occurred. ERROR: database is being accessed by other users DETAIL: There is other session using the database.

To overcome the above error, we need to force disconnect all connected users via a query and then try deleting the database.

/* Terminate all Database conenctions except current one */SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid();




Once the above query is executed, close the SQL window and then try deleting the database. Closing the SQL window is important as that is considered as one active connection and pgAdmin would still result in the same error, if the window is not closed.
Force Deleting a Postgres Database Force Deleting a Postgres Database Reviewed by Karthik on Wednesday, July 08, 2020 Rating: 5

No comments:

Feel free to leave a piece of your mind.

Powered by Blogger.