如何杀死我所有的postgresql连接?

我试着耙db:下降,但我得到:

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

当前回答

退出postgres并重新启动。很简单,但每次对我都有效,而其他cli命令有时不能。

其他回答

可以使用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'
    ;
SELECT 
pg_terminate_backend(pid) 
FROM 
pg_stat_activity 
WHERE
pid <> pg_backend_pid()
-- no need to kill connections to other databases
AND datname = current_database();
-- use current_database by opening right query tool

OSX, Postgres 9.2(已安装自制程序)

$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ pg_ctl restart -D /usr/local/var/postgres
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

如果你的datadir在其他地方,你可以通过检查ps aux | grep postgres的输出来找到它

我的工作如下:

sudo gitlab-ctl stop
sudo gitlab-ctl start gitaly
sudo gitlab-rake gitlab:setup [type yes and let it finish]
sudo gitlab-ctl start

我正在使用: gitlab_edition:“gitlab-ce” gitlab_version:“12.4.0-ce.0.el7”

我是这样解决的:

在我的Windows8 64位,只是重新启动服务:postgresql-x64-9.5