Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres drop database error: pq: cannot drop the currently open database

Tags:

postgresql

go

I'm trying to drop the database I'm currently connected to like so, but I'm getting this error:

pq: cannot drop the currently open database 

I don't really understand how I'm expected to drop the database if I have to close my connection, because then I don't think I will be able to use dbConn.Exec to execute my DROP DATABASE statement?

dbConn *sql.DB  func stuff() error {   _, err := dbConn.Exec(fmt.Sprintf(`DROP DATABASE %s;`, dbName))   if err != nil {     return err   }    return dbConn.Close() } 

I guess I could connect to a different database and then execute it on that connection, but I'm not even sure if that'd work, and it seems really weird to have to connect to a new database just to drop a different database. Any ideas? Thanks.

like image 578
b0xxed1n Avatar asked Apr 08 '16 14:04

b0xxed1n


People also ask

How do I drop a current open database?

If you are facing a situation, where a different client is connected to the database, and you really want to drop the database, you can forcibly disconnect all the client from that particular database. SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';

How do I force drop a PostgreSQL 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. Similarly, DROP DATABASE FORCE will do the same.

How do I drop a database in PgAdmin?

Drop Database PgAdmin (Graphical User interface)Select the database (Javatpoint) by a left-click on it. After that, click on Delete/dropoption from the given drop-down list to delete the database.


2 Answers

Because, you are trying to execute dropDb command on database, to which you have open connection.

According to postgres documentation:

You cannot be connected to the database you are about to remove. Instead, connect to template1 or any other database and run this command again.

This makes sense, because when you drop the entire database, all the open connection referencing to that database becomes invalid, So the recommended approach is to connect to different database, and execute this command again.

If you are facing a situation, where a different client is connected to the database, and you really want to drop the database, you can forcibly disconnect all the client from that particular database.

For example, to forcibly disconnect all clients from database mydb:

If PostgreSQL < 9.2

SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = 'mydb';

Else

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';

Note: This command requires superuser privileges.

Then, you can connect to different database, and run dropDb command again.

like image 153
Parth Desai Avatar answered Oct 03 '22 07:10

Parth Desai


If you encounter this problem in IntelliJ, change the schema with the following dropdown to postgres.

After that, I was able to drop a db.

enter image description here

like image 26
simibac Avatar answered Oct 03 '22 07:10

simibac