如何杀死所有我的postgresql连接?
我试图一个rake db:drop但我得到:
ERROR: database "database_name" is being accessed by other users DETAIL: There are 1 other session(s) using the database.
我试过关闭我从一个ps -ef |看到的进程grep postgres但这不工作:
kill: kill 2358 Failed: operation not permitted
您可以使用
pg_terminate_backend()来终止连接。您必须是超级用户才能使用此功能。这在所有操作系统上都是一样的。
原文链接:https://www.f2er.com/postgresql/194024.htmlSELECT 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' ;
在执行此查询之前,您必须具有REVOKE的CONNECT权限才能避免新连接:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC,username;
If you’re using Postgres 8.4-9.1 use procpid instead of 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' ;