Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I need exclusive access to a database. Is it possible using an SQL command to "detach" all other users from a postgres database. Or maybe closing all other connections and then gaining exclusive access.

This is for unit testing, and tests are only run manually, so there is no danger involved. Only old dead connections will be affected.

There are no other users connecting to these unittest databases.

The old dead connections come from developing. This happens all the time when a test that is being written or fails does not exit clean.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

You could try connecting to the database as the postgres user and running:

SELECT pg_terminate_backend( procpid )
FROM pg_stat_activity
WHERE procpid <> pg_backend_pid( )    -- 1. don't terminate your own session
    AND datname =                     -- 2. don't terminate connections to 
    (SELECT datname                   --    other databases in the cluster
       FROM pg_stat_activity
      WHERE procpid = pg_backend_pid( )
    );

update An even better query gets rid of the subselect:

SELECT pg_terminate_backend( procpid )
FROM pg_stat_activity
WHERE procpid <> pg_backend_pid( )
    AND datname = current_database( );
share|improve this answer
2  
don't forget to REVOKE the CONNECT permissions, otherwise the users create new connections before you have exclusive access. –  Frank Heikens Sep 26 '11 at 18:19
    
@Frank Heikens - Good catch. I had keyed on "manual unit test" but if there are others connecting besides the individual doing the unit test then "revoke connect on <datname> from ..." would be essential. –  gsiems Sep 26 '11 at 20:53
    
In PostgreSQL 9.2, procpid was renamed to pid, so watch out for that. –  Craig Ringer Nov 6 '12 at 5:48
    
Beyond doing a REVOKE with the user in question, I had to also REVOKE ..... public - something to watch out for! –  David N. Welton yesterday

The issue here is twofold, first you need to disconnect those users, and second you need to keep them out of your server. Rather than revoking connect perms, I usually use pg_hba.conf to refuse new connects from certain machines and / or users, then just do a pg_ctl -m fast stop;pg_ctl start to drop all current connections. With slony doing DDL changes this is pretty much a necessity or you'll get deadlocks all over the place.

share|improve this answer
4  
I always use a single role that allows CONNECT and is inherited by all other roles. REVOKE connect for this single role and you're done. Wrap it in a function with pg_terminate_backend() and you're in control when you have to stop all current connections. –  Frank Heikens Sep 27 '11 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.