终止 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
How can I kill all my postgresql connections?
I'm trying a rake db:drop
but I get:
ERROR: database "database_name" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
I've tried shutting down the processes I see from a ps -ef | grep postgres
but this doesn't work either:
kill: kill 2358 failed: operation not permitted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
您可以使用 pg_terminate_backend() 终止连接。您必须是超级用户才能使用此功能。这在所有操作系统上都是一样的。
在执行此查询之前,您必须REVOKE CONNECT 权限以避免新连接:
You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.
Before executing this query, you have to REVOKE the CONNECT privileges to avoid new connections:
也许只需重新启动
postgres
=>sudo 服务 postgresql 重新启动
Maybe just restart
postgres
=>sudo service postgresql restart
有关运行过程的所有信息:
With all infos about the running process:
MacOS,如果 postgresql 与 brew 一起安装:
来源:终止 postgresql 会话/连接
MacOS, if postgresql was installed with brew:
Source: Kill a postgresql session/connection
更简单、更新的方法是:
sudo Kill -9 "#"
该连接注意:可能有相同的 PID。杀死一个人就等于杀死所有人。
Easier and more updated way is:
ps -ef | grep postgres
to find the connection #sudo kill -9 "#"
of the connectionNote: There may be identical PID. Killing one kills all.
OSX、Postgres 9.2(使用自制程序安装)
如果您的数据目录在其他地方,您可以通过检查 ps aux | 的输出来找出它在哪里。 grep postgres
OSX, Postgres 9.2 (installed with homebrew)
If your datadir is elsewhere you can find out where it is by examining the output of
ps aux | grep postgres
如果您需要断开特定用户的会话,这对我有帮助:
检查所有当前连接:
向您的用户授予角色(不重要):
终止会话:
If you need to disconnect sessions of a particular user, this helped me:
Check all current connections:
Grant a role to your user (not important):
Kill sessions:
这似乎适用于 PostgreSQL 9.1:
从 此处 和 此处。
这是一个修改版本,适用于 PostgreSQL 9.1 和 9.2。
This seems to be working for PostgreSQL 9.1:
Lifted from gists found here and here.
Here's a modified version that works for both PostgreSQL 9.1 and 9.2.
MacOS,如果postgresql是用brew:
UBUNTU安装的,
首先检查这个(杀死后台运行的服务器)
如果没有找到pid那么你只需要重新启动
提到的 Postgresql 服务命令如下:
MacOS, if postgresql was installed with brew:
UBUNTU,
firstly check with this (kill server which is running in background)
if you didn't find pid Then you just need to restart
Postgresql service by command which is mention are as under:
我使用以下 rake 任务来覆盖 Rails
drop_database
方法。lib/database.rake
编辑:这适用于 Postgresql 9.2+
I use the following rake task to override the Rails
drop_database
method.lib/database.rake
Edit: This is for Postgresql 9.2+
我遇到了这个问题,问题是 Navicat 连接到了我本地的 Postgres 数据库。一旦我断开 Navicat 的连接,问题就消失了。
编辑:
此外,作为绝对的最后手段,您可以备份数据,然后运行此命令:
...这将杀死postgres 用户正在访问的所有内容。避免在生产计算机上执行此操作,但在开发环境中应该不会有问题。在此之后尝试重新启动 PostgreSQL 之前,确保每个
postgres
进程确实终止是至关重要的。编辑2:
由于 这篇 unix.SE 帖子,我已从
kill -9
更改为kill -15
。I had this issue and the problem was that Navicat was connected to my local Postgres db. Once I disconnected Navicat the problem disappeared.
EDIT:
Also, as an absolute last resort you can back up your data then run this command:
... which will kill everything that the postgres user is accessing. Avoid doing this on a production machine but you shouldn't have a problem with a development environment. It is vital that you ensure every
postgres
process has really terminated before attempting to restart PostgreSQL after this.EDIT 2:
Due to this unix.SE post I've changed from
kill -9
tokill -15
.我这样解决了:
在我的 Windows8 64 位中,只需
重新启动
服务:postgresql-x64-9.5I'VE SOLVED THIS WAY:
In my Windows8 64 bit, just
restart
ing the service: postgresql-x64-9.5在 PG 管理中,您可以断开服务器连接(右键单击服务器)&所有会话将在重新启动时断开连接
In PG admin you can disconnect your server (right click on the server) & all sessions will be disconnected at restart
首先,找到要关闭的查询/连接的 PID(请参阅 如何列出 PostgreSQL 上的活动连接?)
然后,像这样关闭与您选择的 PID 的连接
First, locate the PID of the query/connection you want to close (see How to list active connections on PostgreSQL?)
Then, closing the connection with the PID you picked like this
只是想指出,如果其他后台进程正在使用数据库,哈里斯的答案可能不起作用,在我的情况下,它是延迟的作业,我这样做了:
只有这样我才能删除/重置数据库。
Just wanted to point out that Haris's Answer might not work if some other background process is using the database, in my case it was delayed jobs, I did:
And only then I was able to drop/reset the database.
退出 postgres 并重新启动它。很简单,但每次都对我有用,而其他 cli 命令有时却不起作用。
Quit postgres and restart it. Simple, but works every time for me, where other cli commands sometimes don't.
没有必要丢弃它。只需删除并重新创建公共架构即可。在大多数情况下,这具有完全相同的效果。
There is no need to drop it. Just delete and recreate the public schema. In most cases this have exactly the same effect.
远程场景。但是,如果您尝试在 Rails 应用程序中运行测试,并且会收到类似
“ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: 数据库“myapp_test”正在被其他用户访问” 的信息
详细信息:还有 1 个其他会话正在使用该数据库。”
确保在运行测试之前关闭 pgAdmin 或任何其他 postgres GUI 工具。
Remote scenario. But if you're trying to run tests in a rails app, and you get something like
"ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: database "myapp_test" is being accessed by other users
DETAIL: There is 1 other session using the database."
Make sure you close pgAdmin or any other postgres GUI tools before running tests.
案例:
无法执行查询:
解决方案:
一个。显示查询状态活动如下:
b.查找“查询”列包含的行:
c.在同一行中,获取“PID”列
d 的值。执行这些脚本:
Case :
Fail to execute the query :
Solution :
a. Display query Status Activity as follow :
b. Find row where 'Query' column has contains :
c. In the same row, get value of 'PID' Column
d. Execute these scripts :
我使用的是 Mac,通过
Postgres.app
使用 postgres。我解决了这个问题,只是退出并重新启动应用程序。I'm on a mac and I use postgres via
Postgres.app
. I solved this problem just quitting and starting again the app.打开 PGadmin 看看是否有任何查询页面打开,关闭所有查询页面并断开 PostgresSQL 服务器并再次连接并尝试删除/删除选项。这对我有帮助。
Open PGadmin see if there is any query page open, close all query page and disconnect the PostgresSQL server and Connect it again and try delete/drop option.This helped me.
上面的答案之一无疑给了我在 Windows 中解决这个问题的想法。
从 Windows 打开服务,找到 Postgres 服务并重新启动它。
Definitely one of the answers above gave me the idea for solving it in Windows.
Open the Services from Windows, locate the Postgres service and restart it.
对我来说,做了以下工作:
我正在使用:
gitlab_edition:“gitlab-ce”
gitlab_版本:'12.4.0-ce.0.el7'
For me worked the following:
I am using:
gitlab_edition: "gitlab-ce"
gitlab_version: '12.4.0-ce.0.el7'
答案隐藏在上面的评论之一中:
brew services restart postgresql
the answer is hidden in one of the comments above:
brew services restart postgresql