Restricting users to only one session each
If resources need to be closely controlled, you may wish to restrict users so that they can only connect to the server once, at most. The same technique can be used to prevent connections entirely for that user.
How to do it…
We can restrict users to only one connection using the following command:
postgres=# ALTER ROLE fred CONNECTION LIMIT 1; ALTER ROLE
This will then cause any additional connections to receive the following error message:
FATAL: too many connections for role "fred"
You can eliminate this restriction by setting the value to -1
.
It's possible to set the limit to zero or any positive integer. You can set this to a number other than max_connections
, though it is up to you to make sense of that if you do.
Setting the value to zero will completely restrict normal connections. Note that even if you set the connection limit to zero for superusers, they will...