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

前端 未结 17 1634
一向
一向 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条回答
  • 2020-12-12 09:02

    Stop your running application.(in Eclipse) After you try again.

    0 讨论(0)
  • 2020-12-12 09:05

    GUI solution using pgAdmin 4

    First enable show activity on dashboard if you haven't:

    File > Preferences > Dashboards > Display > Show Activity > true
    

    Now disable all the processes using the db:

    1. Click the DB name
    2. Click Dashboard > Sessions
    3. Click refresh icon
    4. Click the delete (x) icon beside each process to end them

    You should now be able to delete the db.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-12-12 09:12

    Solution:
    1. Shut down Pg server

    2. It will disconnect all active connection
    3. Restart Pg Server
    4. Try your command

    0 讨论(0)
  • 2020-12-12 09:13

    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.
    

    First You need to revoke

    REVOKE CONNECT ON DATABASE TARGET_DB FROM public;
    

    Then use:

    SELECT pg_terminate_backend(pg_stat_activity.pid)
    FROM pg_stat_activity
    WHERE pg_stat_activity.datname = 'TARGET_DB';
    

    It will surely work.

    0 讨论(0)
  • 2020-12-12 09:13

    Simply check what is the connection, where it's coming from. You can see all this in:

    SELECT * FROM pg_stat_activity WHERE datname = 'TARGET_DB';
    

    Perhaps it is your connection?

    0 讨论(0)
提交回复
热议问题