Search code examples
postgresqlpostgresql-8.4

postgresql - can't create database - OperationalError: source database "template1" is being accessed by other users


I logged in to source database template1 and now I can't create database. When I try to create database, I get this error:

OperationalError: source database "template1" is being accessed by other users
DETAIL:  There are 5 other session(s) using the database.

Every time I login to template1, I use 'exit' command to logout, but as you can see it does not logout and number of sessions increases everytime I login. Is there a way to force disconnect every connection to template1 that logged in now?


Solution

  • This helped me solve my problem:

    SELECT *, pg_terminate_backend(procpid) 
    FROM pg_stat_activity 
    WHERE usename='username';
    
    --Use pid if PostgreSQL version 9.2 or above.
    

    I terminated all active connections to template1 and could create database normally