Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the copy-the-code domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/pcx3.com/wp-includes/functions.php on line 6121
⚠️ Restrict PostgreSQL users to only one session each - PC✗3
⚠️ Restrict PostgreSQL users to only one session each

⚠️ Restrict PostgreSQL users to only one session each

Recently we had a case where we needed to limit the PostgreSQL user activities to only a single connection per user, and in this article I’m going to share the experience with you.

The connection limit is applied during the session connection. Raising this limit will never
affect any connected users. Lowering the limit doesn’t have any effect either unless they try
to disconnect and reconnect.

The same technique can be used to prevent connections entirely for any user.

We can restrict users to only one connection using the following command:

ALTER ROLE fred CONNECTION LIMIT 1;
ALTER ROLE

This will then cause any additional connections to receive the 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 0 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 0 will completely restrict normal connections. Note that even if you
set the connection limit to zero for super users, they will still be able to connect.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.