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

后端 未结 4 988
别那么骄傲
别那么骄傲 2021-02-05 01:21

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

4条回答
  •  猫巷女王i
    2021-02-05 02:22

    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:\> 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='mydb';
    postgres-# SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb';
    

    Finally, I successfully dropped mydb:

    postgres-# DROP DATABASE mydb;
    

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

提交回复
热议问题