phppgadmin :它如何将用户踢出 postgres,以便可以 db_drop?

发布于 2024-08-24 21:09:04 字数 597 浏览 5 评论 0原文

我有一个 Posgresql 数据库(我是所有者),我想删除它并从转储中重新创建它。

问题是,有几个应用程序(两个网站,rails 和 perl)定期访问数据库。所以我收到“数据库正在被其他用户访问”错误。

我读到一种可能性是获取所涉及进程的 pid 并单独杀死它们。如果可能的话,我想做一些更干净的事情。

Phppgadmin 似乎做了我想做的事:即使网站打开,我也可以使用其 Web 界面删除架构,而不会出现错误。所以我正在研究它的代码是如何工作的。但是,我不是 PHP 专家。

我试图理解 phppgadmin 代码,以便了解它是如何工作的。我发现一行 ( 257 in Schemas.php)其中显示:

$data->dropSchema(...)

$data 是一个全局变量,我找不到它的定义位置。

任何指示将不胜感激。

I've got one Posgresql database (I'm the owner) and I'd like to drop it and re-create it from a dump.

Problem is, there're a couple applications (two websites, rails and perl) that access the db regularly. So I get a "database is being accessed by other users" error.

I've read that one possibility is getting the pids of the processes involved and killing them individually. I'd like to do something cleaner, if possible.

Phppgadmin seems to do what I want: I am able to drop schemas using its web interface, even when the websites are on, without getting errors. So I'm investigating how its code works. However, I'm no PHP expert.

I'm trying to understand the phppgadmin code in order to see how it does it. I found out a line (257 in Schemas.php) where it says:

$data->dropSchema(...)

$data is a global variable and I could not find where it is defined.

Any pointers would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-08-31 21:09:04

首先,找到所有当前使用数据库的进程:

SELECT usename, procpid FROM pg_stat_activity WHERE datname = current_database();

其次,终止您不需要的进程:

SELECT pg_terminate_backend(your_procpid);

这在 8.4 版本中有效,否则 pg_terminate_backend() 是未知的,您必须在操作系统级别终止该进程。


要快速删除连接到给定数据库的所有连接,此快捷方式效果很好。必须以超级用户身份运行:

SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='YourDB';

在较新的 Postgres 版本(至少 9.2+,可能更早)上,列名称已更改,查询为:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='YourDB';

First, find all current procesid's using your database:

SELECT usename, procpid FROM pg_stat_activity WHERE datname = current_database();

Second, kill the processes you don't want:

SELECT pg_terminate_backend(your_procpid);

This works as of version 8.4, otherwise pg_terminate_backend() is unknown and you have to kill the process at OS level.


To quickly drop all connections connected to a given database, this shortcut works nicely. Must run as superuser:

SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='YourDB';

On more recent Postgres versions (at least 9.2+, likely earlier), the column names have changed and the query is:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='YourDB';
嘴硬脾气大 2024-08-31 21:09:04

不确定 PostgreSQL,但我认为可能的解决方案是锁定表,这样其他进程在尝试访问它时就会失败。

看:
http://www.postgresql.org/docs/current/static/ sql-lock.html

Not sure about PostgreSQL but i think a possible solution would be to lock the table so other processes will fail when they try to access it.

See:
http://www.postgresql.org/docs/current/static/sql-lock.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文