Kill a postgresql session/connection

前端 未结 20 1818
暖寄归人
暖寄归人 2020-11-28 00:11

How can I kill all my postgresql connections?

I\'m trying a rake db:drop but I get:

ERROR:  database \"database_name\" is being accessed         


        
相关标签:
20条回答
  • 2020-11-28 00:43

    Quit postgres and restart it. Simple, but works every time for me, where other cli commands sometimes don't.

    0 讨论(0)
  • 2020-11-28 00:46

    With all infos about the running process:

    SELECT *, pg_terminate_backend(pid)
    FROM pg_stat_activity 
    WHERE pid <> pg_backend_pid()
    AND datname = 'my_database_name';
    
    0 讨论(0)
  • 2020-11-28 00:46

    I had this issue and the problem was that Navicat was connected to my local Postgres db. Once I disconnected Navicat the problem disappeared.

    EDIT:

    Also, as an absolute last resort you can back up your data then run this command:

    sudo kill -15 `ps -u postgres -o pid`
    

    ... which will kill everything that the postgres user is accessing. Avoid doing this on a production machine but you shouldn't have a problem with a development environment. It is vital that you ensure every postgres process has really terminated before attempting to restart PostgreSQL after this.

    EDIT 2:

    Due to this unix.SE post I've changed from kill -9 to kill -15.

    0 讨论(0)
  • 2020-11-28 00:50

    OSX, Postgres 9.2 (installed with homebrew)

    $ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    $ pg_ctl restart -D /usr/local/var/postgres
    $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
    


    If your datadir is elsewhere you can find out where it is by examining the output of ps aux | grep postgres

    0 讨论(0)
  • 2020-11-28 00:50
    SELECT 
    pg_terminate_backend(pid) 
    FROM 
    pg_stat_activity 
    WHERE
    pid <> pg_backend_pid()
    -- no need to kill connections to other databases
    AND datname = current_database();
    -- use current_database by opening right query tool
    
    0 讨论(0)
  • 2020-11-28 00:52

    I'VE SOLVED THIS WAY:

    In my Windows8 64 bit, just restarting the service: postgresql-x64-9.5

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