无法删除postgresql数据库

时间:2016-12-20 04:24:05

标签: linux database postgresql

我正在尝试删除数据库。我重启了Postgresql服务:

sudo systemctl stop postgresql
sudo systemctl start postgresql

然而:

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

为什么这样以及如何删除它?

这也不起作用:

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

因为我运行后仍然存在活动连接;

2 个答案:

答案 0 :(得分:1)

你在终端试过吗?

sudo dropdb my_db1

查看man dropdb了解详情。

答案 1 :(得分:0)

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;