如果存在活动连接,如何删除 PostgreSQL 数据库?
我需要编写一个脚本来删除 PostgreSQL 数据库。可能有很多连接,但脚本应该忽略它。
当存在打开的连接时,标准 DROP DATABASE db_name
查询不起作用。
我该如何解决这个问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这将删除除您之外的现有连接:
查询 pg_stat_activity 并获取您想要终止的 pid 值,然后向它们发出 SELECT pg_terminate_backend(pid int) 。
PostgreSQL 9.2 及更高版本:
PostgreSQL 9.1 及更低版本:
一旦您断开所有连接,您将必须断开连接并从另一个数据库(也不是您的数据库)的连接发出 DROP DATABASE 命令。试图下降。
请注意将
procpid
列重命名为pid
。请参阅此邮件列表主题。This will drop existing connections except for yours:
Query
pg_stat_activity
and get the pid values you want to kill, then issueSELECT pg_terminate_backend(pid int)
to them.PostgreSQL 9.2 and above:
PostgreSQL 9.1 and below:
Once you disconnect everyone you will have to disconnect and issue the DROP DATABASE command from a connection from another database aka not the one your trying to drop.
Note the renaming of the
procpid
column topid
. See this mailing list thread.在 PostgreSQL 9.2 及更高版本中,要断开除会话之外的所有内容与所连接的数据库的连接:
在旧版本中是相同的,只需将
pid
更改为procpid
。要断开与其他数据库的连接,只需将current_database()
更改为您想要断开用户连接的数据库的名称。在断开用户连接之前,您可能需要
REVOKE
数据库用户的CONNECT
权限,否则用户将继续重新连接,而您将永远没有机会删除数据库。请参阅此评论 以及与之相关的问题,如何从数据库中分离所有其他用户。如果您只想断开空闲用户的连接,请参阅此问题。
In PostgreSQL 9.2 and above, to disconnect everything except your session from the database you are connected to:
In older versions it's the same, just change
pid
toprocpid
. To disconnect from a different database just changecurrent_database()
to the name of the database you want to disconnect users from.You may want to
REVOKE
theCONNECT
right from users of the database before disconnecting users, otherwise users will just keep on reconnecting and you'll never get the chance to drop the DB. See this comment and the question it's associated with, How do I detach all other users from the database.If you just want to disconnect idle users, see this question.
PostgreSQL 13 引入了
FORCE
选项。PostgreSQL 13 introduced
FORCE
option.Easy Peasy。
我只是在 Ubuntu 中重新启动服务来断开连接的客户端。
Easy Peasy.
I just restart the service in Ubuntu to disconnect connected clients.
您可以使用 pg_terminate_backend(int) 函数在删除数据库之前终止所有连接。
您可以使用系统视图 pg_stat_activity 获取所有正在运行的后端
我不完全确定,但以下内容可能会终止所有会话:
当然,您可能没有将自己连接到该数据库
You could kill all connections before dropping the database using the
pg_terminate_backend(int)
function.You can get all running backends using the system view
pg_stat_activity
I'm not entirely sure, but the following would probably kill all sessions:
Of course you may not be connected yourself to that database
根据您的 postgresql 版本,您可能会遇到一个错误,该错误会使 pg_stat_activity 忽略掉线用户的活动连接。这些连接也不会显示在 pgAdminIII 内。
如果您正在进行自动测试(您还创建用户),这可能是一种可能的情况。
在这种情况下,您需要恢复到如下查询:
注意:在 9.2+ 中,您将把
procpid
更改为pid
。Depending on your version of postgresql you might run into a bug, that makes
pg_stat_activity
to omit active connections from dropped users. These connections are also not shown inside pgAdminIII.If you are doing automatic testing (in which you also create users) this might be a probable scenario.
In this case you need to revert to queries like:
NOTE: In 9.2+ you'll have change
procpid
topid
.我注意到 postgres 9.2 现在调用列 pid 而不是 procpid。
我倾向于从 shell 中调用它:
希望有帮助。感谢 @JustBob 的 sql。
I noticed that postgres 9.2 now calls the column pid rather than procpid.
I tend to call it from the shell:
Hope that is helpful. Thanks to @JustBob for the sql.
PostgreSQL 9.2 及更高版本:
SELECT pg_terminate_backend(pid)FROM pg_stat_activity WHERE datname = 'YOUR_DATABASE_NAME_HERE'
PostgreSQL 9.2 and above:
SELECT pg_terminate_backend(pid)FROM pg_stat_activity WHERE datname = 'YOUR_DATABASE_NAME_HERE'
这是我的黑客... =D
我提出这个答案是因为包含一个命令(上面)来阻止新连接,并且因为任何使用该命令的尝试...
...都无法阻止新连接!
感谢@araqnid @GoatWalker! =D
https://stackoverflow.com/a/3185413/3223785
Here's my hack... =D
I put this answer because include a command (above) to block new connections and because any attempt with the command...
... do not works to block new connections!
Thanks to @araqnid @GoatWalker ! =D
https://stackoverflow.com/a/3185413/3223785
在 Linux 命令提示符中,我首先通过绑定此命令来停止正在运行的所有 postgresql 进程
sudo /etc/init.d/postgresql restart
键入命令
bg 检查其他 postgresql 进程是否仍在运行
,然后使用 dropdb dbname 删除数据库
这在 Linux 命令提示符下对我有用
In Linux command Prompt, I would first stop all postgresql processes that are running by tying this command
sudo /etc/init.d/postgresql restart
type the command
bg to check if other postgresql processes are still running
then followed by dropdb dbname to drop the database
This works for me on linux command prompt
就我而言,我必须执行一个命令来删除所有连接,包括
终止所有连接的活动管理员连接,并向我显示致命的“错误”消息:
FATAL:由于管理员命令 SQL 状态而终止连接:57P01< /code>
之后就可以删除数据库了
In my case i had to execute a command to drop all connections including my active administrator connection
which terminated all connections and show me a fatal ''error'' message :
FATAL: terminating connection due to administrator command SQL state: 57P01
After that it was possible to drop the database
除了我使用 pgAdmin4 登录并在仪表板上断开了除 pgAdmin4 之外的所有连接,然后能够通过右键单击数据库和属性来重命名并输入新名称之外,没有任何效果对我有用。
Nothing worked for me except, I loggined using pgAdmin4 and on the Dashboard I disconnected all connections except pgAdmin4 and then was able to rename by right lick on the database and properties and typed new name.