Postgres, Databases, Heroku

I recently deployed an app to Heroku using Postgres free tier as database.
I am using Django 3.2
Everything was working fine until I started getting a mail with the following error:

Internal Server Error: /
OperationalError at /
FATAL:  remaining connection slots are reserved for non-replication superuser connections

This happens anytime I open the site. It just started doing this.
I thought waiting a while would solve the problem, but it didn’t.

  1. What does this error mean?
  2. What causes the error?
  3. How can I solve it?

My CONN_MAX_AGE is set to 500.

Hi Daveson217,

This means that you don’t have enough database connections to fulfill the requests. Reverting your change to CONN_MAX_AGE to be 0 so that it only lasts the lifetime of the request should solve it. Unless you have many dynos and expect a large load on the site, though in that case upgrading the database should be in order.

Alternatively, you can look into using connection pooling. Heroku provides a pgbouncer buildpack that will help.

Coming back to this, I think the issue is more likely the number of connections rather than CONN_MAX_AGE as per these docs. How many web and worker dynos do you have running?