postgresql Postgres 删除数据库错误:pq:无法删除当前打开的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36502401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:13:18  来源:igfitidea点击:

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

postgresqlgo

提问by b0xxed1n

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.Exec 来执行我的 DROP DATABASE 语句?

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.

我想我可以连接到不同的数据库,然后在该连接上执行它,但我什至不确定这是否可行,而且为了删除不同的数据库而必须连接到新数据库似乎真的很奇怪。有任何想法吗?谢谢。

回答by desaiparth

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

因为,您正在尝试对dropDb已打开连接的数据库执行命令。

According to postgres documentation:

根据 postgres 文档:

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.

您无法连接到要删除的数据库。相反,连接到 template1 或任何其他数据库并再次运行此命令。

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:

例如,强制断开所有客户端与数据库的连接mydb

If PostgreSQL < 9.2

如果 PostgreSQL < 9.2

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

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';

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 dropDbcommand again.

然后,您可以连接到不同的数据库,并dropDb再次运行命令。

回答by Harvey

None of this worked for me since I tried to do it through pgAdmin which kept database connections open as soon as I delete them.

这些都不适合我,因为我试图通过 pgAdmin 来做到这一点,一旦我删除它们,它就会保持数据库连接打开。

Solution:

解决方案:

C:\Program Files\PostgreSQL\scripts\runpsql.bat

after you supply correct information you will be able to get pg command prompt, here you can just type:

提供正确的信息后,您将能够获得 pg 命令提示符,在这里您只需键入:

dbdrop yourdatabase

After that you might still see database in pgAdmin but now you can just simply delete it with right click and DELETE/DROPoption.

之后您可能仍会在 pgAdmin 中看到数据库,但现在您只需右键单击和DELETE/DROP选项即可将其删除。