Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a database in pgadmin

Tags:

java

sql

database

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?

like image 522
Sabrina MK Avatar asked Nov 16 '19 20:11

Sabrina MK


People also ask

How do I delete a PostgreSQL database?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.

How do you delete a database?

In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand Databases, right-click the database to delete, and then click Delete. Confirm the correct database is selected, and then click OK.

How do I force delete a database in PostgreSQL?

A new command-line option is added to dropdb command, and a similar SQL option “FORCE” is also added in DROP DATABASE. Using the option -f or –force with dropdb command or FORCE with DROP DATABASE to drop the database, it will terminate all existing connections with the database.


4 Answers

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

like image 52
softi Avatar answered Oct 06 '22 18:10

softi


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

like image 31
mZed Avatar answered Oct 06 '22 16:10

mZed


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!

like image 36
Ambassel Avatar answered Oct 06 '22 18:10

Ambassel


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

like image 45
dodiws Avatar answered Oct 06 '22 17:10

dodiws