Forcing NOLOGIN users to disconnect

In order to make sure that all users whose login privilege has been revoked are disconnected right away, run the following SQL statement as a superuser:

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity a
JOIN pg_roles r ON a.usename = r.rolname AND NOT rolcanlogin;

This disconnects all users who no longer are allowed to connect by terminating the backends opened by these users.