Heroku "psql: FATAL: remaining connection slots are reserved for non-replication superuser connections"

PostgresqlHeroku

Postgresql Problem Overview


I'm developing an app on Heroku with a Postgresql backend. Periodically, I get this error message when trying to access the database, both from the CLI and from loading a page on the server:

psql: FATAL: remaining connection slots are reserved for non-replication superuser connections

Anyone seen this before or please help point me in the right direction?

Postgresql Solutions


Solution 1 - Postgresql

You either need to increase the max_connections configuration setting or (probably better) use connection pooling to route a large number of user requests through a smaller connection pool.

https://wiki.postgresql.org/wiki/Number_Of_Database_Connections

Solution 2 - Postgresql

This exception happened when I forgot to close the connections

Solution 3 - Postgresql

See https://stackoverflow.com/questions/13640871/heroku-psql-fatal-remaining-connection-slots-are-reserved-for-non-replication:

Heroku sometimes has a problem with database load balancing.

André Laszlo, markshiz and me all reported dealing with that in comments on the question.

To save you the support call, here's the response I got from Heroku Support for a similar issue:

> Hello, > > One of the limitations of the hobby tier databases is unannounced maintenance. Many hobby databases run on a single shared server, and we will occasionally need to restart that server for hardware maintenance purposes, or migrate databases to another server for load balancing. When that happens, you'll see an error in your logs or have problems connecting. If the server is restarting, it might take 15 minutes or more for the database to come back online. > > Most apps that maintain a connection pool (like ActiveRecord in Rails) can just open a new connection to the database. However, in some cases an app won't be able to reconnect. If that happens, you can heroku restart your app to bring it back online. > > This is one of the reasons we recommend against running hobby databases for critical production applications. Standard and Premium databases include notifications for downtime events, and are much more performant and stable in general. You can use pg:copy to migrate to a standard or premium plan. > > If this continues, you can try provisioning a new database (on a different server) with heroku addons:add, then use pg:copy to move the data. Keep in mind that hobby tier rules apply to the $9 basic plan as well as the free database. > > Thanks, > Bradley

Solution 4 - Postgresql

I actually tried to implement connection pooling on the django end using:

https://github.com/gmcguire/django-db-pool

but I still received this error, despite lowering the number of connections available to below the standard development DB quota of 20 open connections.

There is an article here about how to move your postgresql database to the free/cheap tier of Amazon RDS. This would allow you to set max_connections higher. This will also allow you to pool connections at the database level using PGBouncer.

https://www.lewagon.com/blog/how-to-migrate-heroku-postgres-database-to-amazon-rds

UPDATE:

Heroku responded to my open ticket and stated that my database was improperly load balanced in their network. They said that improvements to their system should prevent similar problems in the future. Nonetheless, support manually relocated my database and performance is noticeably improved.

Solution 5 - Postgresql

To reproduce same issue in Linux:

for i in {1..300}; do
	 PGPASSWORD=MY_PASSWORD gnome-terminal -e  $'/usr/bin/psql -h \'127.0.0.1\' -p 5432 -U MY_USERNAME' 
done

In a dotnet client you can read:

> System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. > ---> Npgsql.PostgresException (0x80004005): 53300: sorry, too many clients already

Solution 6 - Postgresql

I had a lot of idle connections in my case, so I had to reuse idle connections before creating new ones,

Solution 7 - Postgresql

The error message means that the app has used up all available connections.

While using postgres in aws with knex and typescript to do some query and update job, the problem pops up when it finishes 390 database operations, for which a mistake prevents the normal knex.destroy() operation. The error message is:

(node:66236) UnhandledPromiseRejectionWarning: error: remaining connection slots are reserved for non-replication superuser connections

When knex.destroy() goes to the right place the error is gone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnathancahillView Question on Stackoverflow
Solution 1 - PostgresqlkgrittnView Answer on Stackoverflow
Solution 2 - PostgresqlSanyifejűView Answer on Stackoverflow
Solution 3 - PostgresqlAur SarafView Answer on Stackoverflow
Solution 4 - PostgresqlmarkshizView Answer on Stackoverflow
Solution 5 - PostgresqlprofimedicaView Answer on Stackoverflow
Solution 6 - PostgresqlMuriithi DerrickView Answer on Stackoverflow
Solution 7 - PostgresqlJustin TangView Answer on Stackoverflow