Saurabh
Saurabh

Reputation: 189

Unable to delete a postgresql database

I'm trying to delete a database. I've restart Postgresql service:

sudo systemctl stop postgresql
sudo systemctl start postgresql

And yet:

# drop database my_db1;
ERROR:  database "my_db1" is being accessed by other users

Why is that and how to delete it?

This isn't working either:

select pg_terminate_backend(pg_stat_activity.pid) 
from pg_stat_activity 
where pg_stat_activity.datname = 'my_db1' and pid <> pg_backend_pid();

because active connections still exist after I've run it;

Upvotes: 1

Views: 592

Answers (2)

Karen
Karen

Reputation: 9

REVOKE CONNECT ON DATABASE YourDbName FROM PUBLIC;
    SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = 'YourDbName';
    DROP DATABASE YourDbName;

Upvotes: 0

Bhavik Kheni
Bhavik Kheni

Reputation: 115

have you tried in terminal ?

sudo dropdb my_db1

View man dropdb for more information.

Upvotes: 2

Related Questions