How to set max db connections in postgres

How to set max db connections in postgres

If you have developed a very poor application which creates more and more connections to your database server configured on PostgreSQL and its not releasing the unused connections, Surely you will encounter this problem one day. You need to do this configuration on your Postgres to increase the number of connections to keep in business. Under given is the method to increase the number of connections on Linux and Windows.

How to check the current number of connections?

Use this query:

select min_val,max_val from pg_settings where name='max_connections'

Increase the number of connections on Linux

We use max_connections in the postgresql.conf to set the maximum number of concurrent connections to the database server.

  1. First find your postgresql.conf file
  2. If you don’t know where it is, query the database with the sql: SHOW config_file;
  3. The default is in: /var/lib/pgsql/data/postgresql.conf
  4. Login as root and edit the conf file with vi editor.
  5. Search for the string: “max_connections”.
  6. That like will look like max_connections=20.
  7. Set that number bigger,
  8. Please check the limit for your postgresql version.
  9. Restart the postgresql database for the changes to take effect and you are good to go.

Increase the number of connections on Windows

  1. First find your postgresql.conf file under the installation directory
  2. Go to the configuration file and open with notepad of notepad++
  3. Search for the string “max_connections”.
  4. That line will look like max_connections=20.
  5. change the number to a bigger one. Please check the limit for your postgresql version.
  6. Restart the postgresql database for the changes to take effect and you are good to go.

I hope this will help you solve your problem temporarily as the ultimate solution is to change the code in your application so that it can work with standard number of database connections or at least can work with a defined number of connections.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.