Tell me more ×
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 want to drop all connections (sessions) that are currently opened to a specific postgresql database but without restarting the server or disconnecting connections to other databases.

How can I so that?

share|improve this question
I read somewhere that you could use lowth.com/cutter to achive this. – Krister Andersson Apr 11 '12 at 14:51

migrated from stackoverflow.com Apr 12 '12 at 14:38

2 Answers

up vote 6 down vote accepted

The query like this should help (assuming the database is named 'db'):

select pg_terminate_backend(procpid) from pg_stat_activity where datname='db';

However you have to be a superuser to disconnect other users.

share|improve this answer
2  
It might also be useful to REVOKE CONNECT ON DATABASE FROM PUBLIC or something similar, and then GRANT it afterward. – kgrittn Apr 11 '12 at 15:14

This can be used to "free" a database from client connections, so that you for example can rename it. One-Line-Example:

SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='current_db';ALTER DATABASE current_db RENAME TO old_db; ALTER DATABASE new_db RENAME TO current_db;

Be aware that this might cause problematic behaviour to your client apps. Data actualy should not be currupted due to using transactions.

share|improve this answer

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.