136

I am using Postgres DB for my product. While doing the batch insert using slick 3, I am getting an error message:

org.postgresql.util.PSQLException: FATAL: sorry, too many clients already.

My batch insert operation will be more than thousands of records. Max connection for my postgres is 100.

How to increase the max connections?

Mel
  • 4,929
  • 10
  • 33
  • 39
Jet
  • 2,197
  • 2
  • 26
  • 43
  • http://stackoverflow.com/questions/9798705/arval-sqlexception-fatal-sorry-too-many-clients-already-in-postgres – user_0 Jun 11 '15 at 10:19
  • Use a connection pool like pgBouncer or pgPool – Frank Heikens Jun 11 '15 at 10:19
  • 2
    1. First check if your middleware is not keeping too many connections open, or is leaking them. 2. (maybe) next use a connection pooler. 3. (almost) never increase the number of allowed connections on this type of problem, in most cases this will make things worse. (except if you are absolutely sure) – joop Jun 11 '15 at 11:59
  • See also https://stackoverflow.com/a/58373811/1609929 for an option using SQL commands instead of configuration. – Jethro Dec 04 '20 at 12:49

3 Answers3

273

Just increasing max_connections is bad idea. You need to increase shared_buffers and kernel.shmmax as well.


Considerations

max_connections determines the maximum number of concurrent connections to the database server. The default is typically 100 connections.

Before increasing your connection count you might need to scale up your deployment. But before that, you should consider whether you really need an increased connection limit.

Each PostgreSQL connection consumes RAM for managing the connection or the client using it. The more connections you have, the more RAM you will be using that could instead be used to run the database.

A well-written app typically doesn't need a large number of connections. If you have an app that does need a large number of connections then consider using a tool such as pg_bouncer which can pool connections for you. As each connection consumes RAM, you should be looking to minimize their use.


How to increase max connections

1. Increase max_connection and shared_buffers

in /var/lib/pgsql/{version_number}/data/postgresql.conf

change

max_connections = 100
shared_buffers = 24MB

to

max_connections = 300
shared_buffers = 80MB

The shared_buffers configuration parameter determines how much memory is dedicated to PostgreSQL to use for caching data.

  • If you have a system with 1GB or more of RAM, a reasonable starting value for shared_buffers is 1/4 of the memory in your system.
  • it's unlikely you'll find using more than 40% of RAM to work better than a smaller amount (like 25%)
  • Be aware that if your system or PostgreSQL build is 32-bit, it might not be practical to set shared_buffers above 2 ~ 2.5GB.
  • Note that on Windows, large values for shared_buffers aren't as effective, and you may find better results keeping it relatively low and using the OS cache more instead. On Windows the useful range is 64MB to 512MB.

2. Change kernel.shmmax

You would need to increase kernel max segment size to be slightly larger than the shared_buffers.

In file /etc/sysctl.conf set the parameter as shown below. It will take effect when postgresql reboots (The following line makes the kernel max to 96Mb)

kernel.shmmax=100663296

References

Postgres Max Connections And Shared Buffers

Tuning Your PostgreSQL Server

Blakethepatton
  • 476
  • 5
  • 14
Ankit
  • 4,255
  • 2
  • 18
  • 22
  • great answer. couple of questions.. how is 100663296 equal to 96MB? and why do we change shmmax what does that do? – Robbo_UK Jun 05 '18 at 09:22
  • 3
    100663296 Bytes = 96 MB (in binary). shmmax is *maximum size of a shared memory segment*. Now because we increased size of shared buffers we need to change shmmax to accomodate the increased memory for caching. – Ankit Jun 05 '18 at 10:23
  • 1
    @Robbo_UK 96 * 1024 * 1024 = 100663296 – skrebbel Jun 11 '18 at 11:44
  • 10
    You may use [PGTune](https://pgtune.leopard.in.ua/) to help you determine these settings for your system – Yoan Tournade Jan 02 '19 at 18:23
  • I found this online calculator useful for converting units, [for example 96MB to kB](http://unitconverter.io/megabytes/bytes/96).. – elrobis Feb 25 '19 at 22:02
  • 17
    Check your current kernel.shmmax setting (`cat /proc/sys/kernel/shmmax`) prior to changing it. On modern systems it's already set ridiculously high and should not be changed. Mine is set to `18446744073692774399` by default on Ubuntu 18.04. – Carl Zulauf Aug 08 '19 at 08:09
49

Adding to Winnie's great answer,

If anyone is not able to find the postgresql.conf file location in your setup, you can always ask the postgres itself.

SHOW config_file;

For me changing the max_connections alone made the trick.

Jinxer Albatross
  • 1,109
  • 12
  • 17
1

change max_connections variable in postgresql.conf file located in /var/lib/pgsql/data or /usr/local/pgsql/data/

Archana
  • 365
  • 4
  • 16
  • 2
    Why pg_hba.conf? It doesn't have any parameter about the number of connections, only how to connect. postgresql.conf on the other hand.... But hundreds of connections is a bad idea, don't do it, use a connection pool or suffer from performance issues. PostgreSQL 8.3 is EOL for many years, please use an up to date version. – Frank Heikens Jun 11 '15 at 10:50