Tuesday, February 11, 2014

PG::ObjectInUse: ERROR: source database "template1" is being accessed by other users

PG::ObjectInUse: ERROR:  source database "template1" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.

Database template1 exists only to use structure to create another empty database. You should never login to template1, otherwise you will have problems for login or create database.

Most likely easiest solution for you is to restart PostgreSQL server process, and logon again. 

For restart PostgreSQL server use this command:
sudo service postgresql restart
If restarting is not working then you can use another template database: template0.

For this log in with username and password in postgresql with this command:
sudo -u postgres psql template1
          Enter password if you used password for user "postgres"

Then change template with database_name 

By default, database create for template1 with this statement and this statement:
CREATE DATABASE database_name;
is equivalent to:
CREATE DATABASE database_name TEMPLATE template1;
If template1 is not available or corrupted, you can use template0 as last resort:
CREATE DATABASE database_name TEMPLATE template0;
You can read more about template databases here.