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.
Restricting users to only one session each
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...