0

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

eadsjr
  • 681
  • 5
  • 20

1 Answers1

1

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';
eadsjr
  • 681
  • 5
  • 20