Force client disconnect using PostgreSQL

前端 未结 6 1121
轮回少年
轮回少年 2020-12-25 11:33

Is there a way to force clients to disconnect from PostgreSQL? I\'m looking for the equivlent of DB2\'s force application all.

I\'d like to do this on my development

6条回答
  •  难免孤独
    2020-12-25 11:57

    This SO answer beautifully explains (full quote from araqnid between the horizontal rules, then me again):


    To mark database 'applogs' as not accepting new connections:

    update pg_database set datallowconn = false where datname = 'applogs';
    

    Another possibility would be to revoke 'connect' access on the database for the client role(s).

    Disconnect users from database = kill backend. So to disconnect all other users from "applogs" database, for example:

    select pg_terminate_backend(procpid)
    from pg_stat_activity
    where datname = 'applogs' and procpid <> pg_backend_pid();
    

    Once you've done both of those, you are the only user connected to 'applogs'. Although there might actually be a delay before the backends actually finish disconnecting?


    Update from MarkJL: There is indeed a delay before the backends finish disconnecting.

    Now me again: That being said, mind that the procpid column was renamed to pid in PostgreSQL 9.2 and later.

    I think that this is much more helpful than the answer by Milen A. Radev which, while technically the same, does not come with usage examples and real-life suggestions.

提交回复
热议问题