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?
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.
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.
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.
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:
Close connections to the databases 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 tab "Properties" on the right hand side
There you can select all your databases you would like to delete and click on the trashcan icon:
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
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!
You can not drop the currently open database. Run query DROP DATABASE db_name;
in Query Tool
from another database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With