Reputation: 721
I was looking for a way to disconnect a single, known bad actor role from a PostgresQL database immediately. I want to prevent them from making further changes as quickly as possible, and don't want them to be able to immediately reconnect and continue. I don't want this to disrupt any other connections that are currently working on the database.
I saw some information here, but it was not clear how to apply that to this case cleanly: Force client disconnect using PostgreSQL
Upvotes: 0
Views: 57
Reputation: 721
As of PostgresQL 11.2, if your problematic role is 'baduser' you can run the following commands in psql.
-- to prevent further connections from this user
ALTER ROLE baduser WITH NOLOGIN;
-- to terminate current connection from this user
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE usename = 'baduser';
Upvotes: 1