15

When i try to create other database with the name "eCommerce" in pgadmin 4 this message appears

ERROR: source database "template1" is being accessed by other users DETAIL: There are 2 other sessions using the database.

I try to delete the others databases but is not working and appears

ERROR: cannot drop a template database

What should i do?

Sabrina MK
  • 155
  • 1
  • 2
  • 6

5 Answers5

37

It's impossible to drop a database in pgAdmin4 by right-clicking on it and selecting Delete/Drop from the context menu. Because as soon as you right-click on it pgAdmin opens a connection to that database.

But there is another way in pgAdmin4:

  1. Close connections to the databases you would like to delete by right-clicking on it and selecting "Disconnect database..."

  2. Left-click on "Databases" (One up in the hierarchy: The folder that contains all your databases)

  3. Select tab "Properties" on the right hand side

  4. There you can select all your databases you would like to delete and click on the trashcan icon:

enter image description here

softi
  • 491
  • 1
  • 4
  • 3
11

You simply need to terminate the connections before deleting the database :)

    //Terminate all Connections on HostDB
     SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'Database_you_want_to_delete';

After that deleting shall work without an further problems.

Edit: For further information please take a look at this thread: Kill a postgresql session/connection

mZed
  • 339
  • 2
  • 7
3

The Simplest Solution, I think, is closing opened tabs

simply closing any opened tabs related to that database in pgAdmin solves it.

In your case, 2 processes "might" be translated as at most two tabs in pgAdmin related to that database!

2

You can not drop the currently open database. Run query DROP DATABASE db_name; in Query Tool from another database.

dodiws
  • 46
  • 3
0

Using pgAdmin, I followed @softi's solution but tweaked it a little bit and it worked

First, Close connections to the database(s) you would like to delete by right-clicking on it and selecting "Disconnect database..."

Left-click on "Databases" (One up in the hierarchy: The folder that contains all your databases)

Select "Dashboard" tab on the right hand side and terminate the session(s) of the database(s) you want to delete/drop

Then go to the "Properties" tab (next to the Dashboard tab), then select the database(s) you would like to delete and click on the trashcan icon above the list