English 中文(简体)
phppgadmin : How does it kick users out of postgres, so it can db_drop?
原标题:

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.

最佳回答

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 ;
问题回答

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





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签