Django + DigitalOcean Managed PostgreS: “remaining connection slots are reserved for roles with the SUPERUSER attribute” when using connection pooling

I’m trying to configure Django to use a connection pool against a DigitalOcean Managed PostgreSQL instance, but keep running into this error:

OperationalError: connection failed: connection to server at "xxxxxxxxxxxx", port yyyy failed: FATAL:
remaining connection slots are reserved for roles with the SUPERUSER attribute

My DATABASES setting looks like this (Django 5.2+):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': database_name,
        'USER': database_user,
        'PASSWORD': database_password,
        'HOST': database_host,
        'PORT': database_port,
        "OPTIONS": {
            'sslmode': sslmode,
            'pool': {
                'min_size': 5,
                'max_size': 150,
                'timeout': 20,
            },
        }
    }
}

Is this the correct way to enable pooling via Django’s built-in OPTIONS['pool'] settings?

Your available number of connections is 25 * [GBs of RAM on Postgres] - 3. Then the maximal number of connections that you use is [number of Django workers] * [max_size set in settings.py]. If the first one is bigger that the second one, then everything will work. See how many workers of Django you run (no way that's only one worker if you are over the limit) and adjust the number.
If you did not set this number, then Gunicorn runs [number of CPUs] * 2 + 1 workers by default. So even 1vCPU on your server would mean that you actually go over the limit.

Вернуться на верх