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

huangapple go评论85阅读模式
英文:

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

问题

我正在尝试像这样删除我当前连接的数据库,但是我遇到了这个错误:

pq: 无法删除当前打开的数据库

我真的不明白如果我必须关闭连接,我应该如何删除数据库,因为那样我就无法使用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'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.

答案1

得分: 82

因为你正在尝试在已经打开连接的数据库上执行dropDb命令。

根据PostgreSQL文档:

你不能连接到即将删除的数据库。相反,连接到template1或任何其他数据库,然后再次运行此命令。

这是有道理的,因为当你删除整个数据库时,所有引用该数据库的打开连接都会变得无效,所以建议的方法是连接到不同的数据库,然后再次执行此命令。

如果你面临这样的情况,即不同的客户端连接到数据库,而你确实想要删除数据库,你可以强制断开所有连接到该特定数据库的客户端。

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

如果 PostgreSQL < 9.2

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

否则

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

**注意:**此命令需要超级用户权限。

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

英文:

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 = &#39;mydb&#39;;

Else

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = &#39;mydb&#39;;

Note: This command requires superuser privileges.

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

答案2

得分: 45

如果您在IntelliJ中遇到此问题,请使用下拉菜单更改模式为postgres。

之后,我能够删除一个数据库。

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

英文:

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.

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

答案3

得分: 13

要删除数据库:

\c postgres

然后执行DROP DATABASE your_database即可。

英文:

To drop the database:

\c postgres

Then
DROP DATABASE your_database works

答案4

得分: 7

我正在使用PostgreSQL 12和Windows 10中的pgAdmin-4。我不得不使用上述答案的组合来删除一个数据库,因为我无法在pgAdmin中删除它,原因是我无法关闭所有打开的连接。

关闭pgAdmin-4。

在Windows命令行中,假设我的服务器名称是postgres,我的数据库是mydb:

C:\> psql -U postgres

我使用我的服务器密码登录。

然后,我关闭了所有连接到mydb的连接:

postgres-# SELECT * FROM pg_stat_activity WHERE pg_stat_activity.datname='mydb';
postgres-# SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';

最后,我成功删除了mydb:

postgres-# DROP DATABASE mydb;

现在,如果我重新进入pgAdmin-4,它已经消失了。

英文:

I am using PostgreSQL 12 and pgAdmin-4 in Windows 10. I had to use a combination of the above answers to drop a database, which I could not drop in pgAdmin because I was unable to close all open connections in pgAdmin.

Close pgAdmin-4.

In Windows command line, assuming my server's name is postgres and my database is mydb:

C:\&gt; psql -U postgres

I logged in with my server password.

I then closed all open connections to mydb:

postgres-# SELECT * FROM pg_stat_activity WHERE pg_stat_activity.datname=&#39;mydb&#39;;
postgres-# SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = &#39;mydb&#39;;

Finally, I successfully dropped mydb:

postgres-# DROP DATABASE mydb;

Now if I go back into pgAdmin-4 it is gone.

答案5

得分: 5

只需使用\c db_name连接到不同的数据库;
然后使用drop database db_name命令删除所需的数据库。

英文:

Just Connect to a different database using \c db_name;
and then drop the required database using drop database db_name;

答案6

得分: 4

很简单,只需连接到另一个数据库\c database2。连接成功后,在连接到的另一个数据库中执行删除数据库的命令。

英文:

It's simple, just connect to another database \c database2. Once connected execute the drop database command while connected to the other database.

答案7

得分: 2

如果你正在使用DBeaver,请确保你没有连接到你想要删除的数据库。进入编辑连接,查看数据库名称。

切换到另一个数据库连接,然后删除你想要删除的数据库。

英文:

If you are using DBeaver make sure you are not connected to the database you are trying to drop. Go to edit connections and look at the database name.

Switch the connection to a different database and then drop the database you wish.

答案8

得分: 1

以下是翻译好的内容:

对我来说,这些方法都不起作用,因为我尝试通过pgAdmin来执行操作,但是一旦我删除数据库连接,它就会保持打开状态。

解决方案:

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

dbdrop yourdatabase

之后,你可能仍然会在pgAdmin中看到数据库,但现在你可以通过右键点击并选择“删除/丢弃”选项来简单地删除它。

英文:

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.

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:

dbdrop yourdatabase

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

答案9

得分: -1

你可以使用以下命令强制删除数据库:DROP DATABASE mydb WITH (FORCE)

英文:

you can force to drop database with: DROP DATABASE mydb WITH (FORCE)

huangapple
  • 本文由 发表于 2016年4月8日 22:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/36502401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定