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.
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!