How to Resolve 'pq: cannot drop the currently open database' Error in PostgreSQL?

Susan Sarandon
Release: 2024-11-15 01:32:02
Original
733 people have browsed it

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';
Copy after login

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

The above is the detailed content of How to Resolve 'pq: cannot drop the currently open database' Error in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template