Postgresql - unable to drop database because of some auto connections to DB

前端 未结 17 1788
一向
一向 2020-12-12 08:18

Whenever I try to drop database I get:

ERROR:  database \"pilot\" is being accessed by other users
DETAIL:  There is 1 other session using the database.
         


        
17条回答
  •  萌比男神i
    2020-12-12 09:09

    You can prevent future connections:

    REVOKE CONNECT ON DATABASE thedb FROM public;
    

    (and possibly other users/roles; see \l+ in psql)

    You can then terminate all connections to this db except your own:

    SELECT pid, pg_terminate_backend(pid) 
    FROM pg_stat_activity 
    WHERE datname = current_database() AND pid <> pg_backend_pid();
    

    On older versions pid was called procpid so you'll have to deal with that.

    Since you've revoked CONNECT rights, whatever was trying to auto-connect should no longer be able to do so.

    You'll now be able to drop the DB.

    This won't work if you're using superuser connections for normal operations, but if you're doing that you need to fix that problem first.


    After you're done dropping the database, if you create the database again, you can execute below command to restore the access

    GRANT CONNECT ON DATABASE thedb TO public;
    

提交回复
热议问题