Error: Could not create database connection

Every once in a while, probably a few times a week, I see the following error in the logs:

    Fatal error: Uncaught exception 'Exception' with message 'Could not create database connection: DBError!:SQLSTATE[08006] [7] FATAL: connection limit exceeded for non-superusers' in /home/websites/puc_matrix_3-16-2/core/include/mysource.inc:2313 
    Stack trace: #0 /home/websites/puc_matrix_3-16-2/core/include/mysource.inc(211): MySource->changeDatabaseConnection('db') #1 /home/websites/puc_matrix_3-16-2/core/include/init.inc(231): MySource->init() #2 /home/websites/puc_matrix_3-16-2/core/web/index.php(28): require_once('/home/websites/...') #3 {main} thrown in /home/websites/puc_matrix_3-16-2/core/include/mysource.inc on line 2313


It does not seem to cause any issues and the site never seems to be down. But, it just worries me since it is not a very friendly error. In reading, it seems that we have too many apache processes, is this the case?

Yes, you've run out of DB connections, which would generally mean you've got too many active Apache processes holding the resources. It could mean your traffic is spiking or it could mean something is hogging resources (a script for example).


The fact that your site looks fine indicates it is probably a traffic spike. It might be worth bumping up the number of allowed connections just a little to see if you get less errors. You don't want to see them because it means someone is seeing a blank page and getting a 500 internal server error.

[quote]
It might be worth bumping up the number of allowed connections just a little to see if you get less errors. You don't want to see them because it means someone is seeing a blank page and getting a 500 internal server error.

[/quote]





You mean in Apache or Postgres?



Also, is this a RAM issue, or just a settings issue?

Sorry, meant PostgreSQL. There is a connection limit setting in the config file (I don't remember where it is).

[quote]
Sorry, meant PostgreSQL. There is a connection limit setting in the config file (I don’t remember where it is).

[/quote]



It’s max_connections in /etc/postgresql/version_number/main/postgresql.conf (for debian/ubuntu) and /var/lib/pgsql/data/main/postgresql.conf (for rhel/centos).



Postgres isn’t really designed to have a high connection limit (postgres spawns a new process I think for each new connection and it has a “high” start up cost - there’s quite a bit of processing involved in doing that) - so if you have to set it over (say) 150 you might be better off putting a connection pool (pgpool or pgbouncer) in front and lowering the number of active connections postgres allows. This has a nice side effect as well - each connection can then utilize more memory.



Also if you increase max_connections you may have to either bump kernel settings up so postgres can use more memory, or lower shared_buffers down (in the same postgresql.conf file) so postgres uses the same amount of memory. See docs on max_connections and docs on max_connections.