database – 终止一个postgresql会话/连接

前端之家收集整理的这篇文章主要介绍了database – 终止一个postgresql会话/连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何杀死所有我的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()来终止连接。您必须是超级用户才能使用此功能。这在所有操作系统上都是一样的。
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'
    ;

在执行此查询之前,您必须具有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'
    ;

猜你在找的Postgre SQL相关文章