我怎样才能杀死我所有的 postgresql 连接?
我正在尝试rake db:drop但我得到:
rake db:drop
ERROR: database "database_name" is being accessed by other users DETAIL: There are 1 other session(s) using the database.
我尝试关闭从 a 看到的进程,ps -ef | grep postgres但这也不起作用:
ps -ef | grep postgres
kill: kill 2358 failed: operation not permitted
您可以使用pg_terminate_backend()来终止连接。您必须是超级用户才能使用此功能。这在所有操作系统上都相同。
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE -- don't kill my own connection! pid <> pg_backend_pid() -- don't kill the connections to other databases AND datname = 'database_name' ;
在执行此查询之前,您必须撤销CONNECT 权限以避免新连接:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;
如果您使用的是 Postgres 8.4-9.1,请使用 procpid 而不是 pid
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE -- don't kill my own connection! procpid <> pg_backend_pid() -- don't kill the connections to other databases AND datname = 'database_name' ;