I have a script in PostgreSQL which restores test database from dump every night. The database is accessed by app servers and processes with connection pool which keeps a fe
If you are connected in session with the DB that you want to dissallow connecting to, and to stay connected in the same session after you disallow connections, then use this :
UPDATE pg_database SET datallowconn = false WHERE datname = '_db_name_' ;
... which allows you to do stuff without any other connection happening until you re-enable
But if you use this, it must be from in a session in another DB :
ALTER DATABASE _db_name_ WITH ALLOW_CONNECTIONS false ;
(( i realise this is almost a summary of the answers above ))