62

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?

Andrius
  • 19,658
  • 37
  • 143
  • 243
  • There is no `exit` command in `psql`. The command to leave `psql` is `\q` (and you shouldn't log in/use the template database in the first place) –  Jan 17 '13 at 08:19
  • I had to log in, because I needed to drop schema public and recreate it to fix one bug. – Andrius Jan 17 '13 at 08:23
  • Also thanks for reply. Now it does not increase in logged in users count, but it stays as 5 users connected. How to force quit these connections? – Andrius Jan 17 '13 at 08:25

19 Answers19

49

Database template1 exists only to provide barebone structure to create another empty database. You should never logon to template1, otherwise you will have problems.

Probably easiest solution for you is to restart PostgreSQL server process, and logon again. Database that should always exist and is safe to logon is postgres.

If restarting is not an option, you can use another emergency template database: template0.

By default, this statement:

CREATE DATABASE dbname;

is equivalent to:

CREATE DATABASE dbname TEMPLATE template1;

If template1 is not available or corrupted, you can use template0 as last resort:

CREATE DATABASE dbname TEMPLATE template0;

You can read more about template databases here.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • I didn't want to log in, but our ERP support told us to do it, to fix one bug..:) – Andrius Jan 17 '13 at 08:31
  • 1
    From doc link: CREATE DATABASE will fail if any other connection to template1 exists. You can use last resort `template0`: `CREATE DATABASE dbname TEMPLATE template0` – mvp Jan 17 '13 at 08:34
  • In older Postgres version there was one reason to log onto the template database: create the plpgsql language in there so that every new database would automatically get it. –  Jan 17 '13 at 08:35
  • So there is no way to clear connections from template1? (I don't have permissions to restart postgresql server, so I asked for our server providers to do that, but its in pending state now) – Andrius Jan 17 '13 at 08:39
  • You should be able to clear them by disconnecting. But, note that template databases are NOT supposed to have any connections, almost ever. If you use `template0`, you don't really need `template1`. I think this is probably the reason why there are 2 template databases - for situations like yours. – mvp Jan 17 '13 at 08:42
  • I create database using Openerp, so it creates database using template1. – Andrius Jan 17 '13 at 08:43
  • What if you create empty database manually (using `psql` or `PgAdmin`), and then handoff this to OpenERP? It should be able to detect that this database is empty, and create necessary tables by itself? – mvp Jan 17 '13 at 08:45
  • I'll try. Also I wanted to ask, is using CTRL+C do not logout from template1? Because in terminal it show as I logged out, but in reality it stayed logged in and messed it like it is now. – Andrius Jan 17 '13 at 08:48
  • If you talking about `psql`, right way to quit is `\q` . BTW, if you really dropped public schema from `template1`, I think it is safe to say that `template1` is now **completely hosed**. You will likely need to recover `template1` by copying it from `template0`. – mvp Jan 17 '13 at 08:51
  • I tried to create db using template0, but I can't use that database in openerp. It says that database already exists – Andrius Jan 17 '13 at 08:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22886/discussion-between-mvp-and-oerp) – mvp Jan 17 '13 at 08:56
38

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

Andrius
  • 19,658
  • 37
  • 143
  • 243
17

You can also try to terminate the current process thread by the Terminal

Search the Process :

sudo ps aux | grep template1

Kill the Process :

sudo kill -9 < your process id >

DASADIYA CHAITANYA
  • 2,850
  • 3
  • 18
  • 41
12

To solve this, I have to disconnect the database connection from the pgAdmin III.

roxdurazo
  • 745
  • 10
  • 21
9

I have a script that connects to a database and performs various operations on it, some requiring that no one else is logged in. I modified @Andrius's answer not to terminate my script's connection, but rather terminate everyone else's:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();

From the docs: pg_backend_pid() is the Process ID of the server process attached to the current session.

Cloud Artisans
  • 4,016
  • 3
  • 30
  • 37
5
  1. enter pgAdmin
  2. right-click to server
  3. disconnect server
  4. again connect server
  5. do what you want
5

If you are using the PgAdminn, you can manually set the definitions provided in the image.

enter image description here

Arefe
  • 11,321
  • 18
  • 114
  • 168
3

You need to kill all the connections to the template database and leave other connections uninterrupted, so use the following query to kill all the connections to templateDb

    SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 
    -- don't kill my own connection!
    pid <> pg_backend_pid()
    -- don't kill the connections to other databases
    AND datname = 'templateDb'
    ;

Then use the usual query to copy the templateDb

CREATE DATABASE dbname TEMPLATE templateDb ;
M.Ali El-Sayed
  • 1,608
  • 20
  • 23
1

This problem occur when you had logged(psql template1 or psql template0) in template1 and template0 database and exit using below command.

Ctrl + z

Better way exist from db use below postgres command then problem will not create:

\q + enter

There are 2 solutions, If have problem.

Solution - 1

Restart posgres service like.

sudo service postgresql restart

Solution - 2

sudo ps aux | grep template1

Make sure don't delete this processes

postgres 8363 0.0 0.0 111760 7832 pts/11 T 09:49 0:00 /usr/lib/postgresql/9.5/bin/psql template1 ankit 18119 0.0 0.0 14224 976 pts/14 S+ 12:33 0:00 grep --color=auto template1

rest of process should be kill using below command.

sudo kill -9

Now try to create db again.

Hope this help you.

Ankit H Gandhi.

1

If you use pgadmin4 or similar tools, makes sure it is either doing the create database itself or that it's shut off.

I had generated a create database with pgadmin4 then used its scripting functionality to export the script it used, which I then copied and altered to put it in my own automated scripts.

Problem was that the active webpage pgadmin somehow ended up on database template1.

Stopping and restarting the server itself didn't fix anything, pgadmin was smart enough to reconnect to the server as soon as it came back up.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73
0

You can try to restart the postgresql service that is running in the background.

ezzadeen
  • 1,033
  • 10
  • 9
0

On Windows I had to reinstall PostgreSQL, restart did not help.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
0

if you go to the side panel , browser, servers, database, then on the right is a summary of the database...becareful here, there is a press right and find the dropdown to delete the database, so if sure, get rid of existing database, and create a new one, so that you can claim ownership of the newly created one

Fioat N
  • 1
  • 1
0

In windows. shutdown the GUI. do it from psql command prompt.

Anupam Kumar
  • 153
  • 2
  • 9
0

I had a same problem but after googling, I understood I have a connection to (postgis_30_sample) database (the template that I want to copy from). This connection was created by GeoServer when I created some stores in Geo Server application.

So I Stopped the service and the issue was fixed ! enter image description here

Mostafa Fallah
  • 398
  • 3
  • 19
0

I have solved problem like this via reconnecting to the server (pgAdmin -> disconnect -> connect)

Tim Uzlov
  • 181
  • 2
  • 6
0

I have the same problem with: ERROR: source database "template1" is being accessed by other users DETAIL: There is 1 other session using the database.

And i Solved the problem in postgreSQL 13 by clicking in the left column on PostgreSQL 13 where the tables showing some graphs appeared. I was interested in the sever activity table, here I found a line that contained the word template1 and I turned it off with a cross, then restarted the application here and everything works.

error: template1-solved

0

If you are using Docker, have to set host to '0.0.0.0' than 'localhost' in your sequelize config.

0

I came across almost same problem. Solution I found is--> "closed dbeaver GUI on my UBUNTU machine and used terminal to create database(lc_db1) using my other database(lc_db) as a TEMPLATE by using following command-->

    CREATE DATABASE lc_db1 TEMPLATE lc_db;

Output of this code is:-->

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 22:16