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

前端 未结 17 1633
一向
一向 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:13

    In terminal try this command:

    ps -ef | grep postgres
    

    you will see like:

    501 1445 3645 0 12:05AM 0:00.03 postgres: sasha dbname [local] idle

    The third number (3645) is PID.

    You can delete this

    sudo kill -9 3645
    

    And after that start your PostgreSQL connection.

    Start manually:

    pg_ctl -D /usr/local/var/postgres start
    
    0 讨论(0)
  • 2020-12-12 09:17

    Simple as that

    sudo service postgresql restart
    
    0 讨论(0)
  • 2020-12-12 09:17

    REVOKE CONNECT will not prevent the connections from the db owner or superuser. So if you don't want anyone to connect the db, follow command may be useful.

    alter database pilot allow_connections = off;
    

    Then use:

    SELECT pg_terminate_backend(pid)
    FROM pg_stat_activity
    WHERE datname = 'pilot';
    
    0 讨论(0)
  • 2020-12-12 09:18

    In my opinion there are some idle queries running in the backgroud.

    1. Try showing running queries first
    SELECT pid, age(clock_timestamp(), query_start), usename, query 
    FROM pg_stat_activity 
    WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' 
    ORDER BY query_start desc;
    
    1. kill idle query ( Check if they are referencing the database in question or you can kill all of them or kill a specific using the pid from the select results )

    SELECT pg_terminate_backend(procpid);

    Note: Killing a select query doesnt make any bad impact

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

    It means another user is accessing the database. Simply restart PostgreSQL. This command will do the trick

    root@kalilinux:~#sudo service postgresql restart
    

    Then try dropping the database:

    postgres=# drop database test_database;
    

    This will do the trick.

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