DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

How to limit the number of connections on the level of the DB user / Database in the Postgres Server?

How to limit the number of connections on the level of DB user in the Postgres Server?

ALTER USER my_db_user WITH CONNECTION LIMIT 2;

SELECT rolname, rolconnlimit
FROM pg_roles
WHERE rolconnlimit <> -1;

This query will return nothing if you don't have any custom roll limits set up.
Enter fullscreen mode Exit fullscreen mode

How to limit the number of connections on the level of the
specific database in the Postgres Server?

postgres=> select datconnlimit from pg_database where datname='my_database';
 datconnlimit 
--------------
            0
(1 row)
Enter fullscreen mode Exit fullscreen mode

Reset to default value (i.e. no limit).

postgres=> alter database my_database connection limit -1;
ALTER DATABASE
postgres=> \q

dmitryr@dmitryr-mac my_postgres % 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)