首頁  >  文章  >  後端開發  >  如何解決 PostgreSQL 中的「pq:無法刪除目前開啟的資料庫」錯誤?

如何解決 PostgreSQL 中的「pq:無法刪除目前開啟的資料庫」錯誤?

Susan Sarandon
Susan Sarandon原創
2024-11-15 01:32:02728瀏覽

How to Resolve

Postgres Drop Database Error: "pq: cannot drop the currently open database"

This error occurs when attempting to drop the database you are currently connected to. According to Postgres documentation, one cannot drop a database that has an open connection. To rectify this issue, connect to a different database and execute the DROP DATABASE command on that connection.

Alternatively, if other clients are connected to the database, you can forcibly disconnect them to allow for the drop operation. This requires superuser privileges, however. To forcibly disconnect clients from a database named "mydb," use the following command:

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

Once all clients have been disconnected, you can connect to a different database and execute the DROP DATABASE command to remove the desired database.

以上是如何解決 PostgreSQL 中的「pq:無法刪除目前開啟的資料庫」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn