Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django django.db.utils.OperationalError: FATAL: remaining connection slots are reserved for non-replication superuser connections

  File "/usr/local/lib/python3.7/dist-packages/psycopg2/__init__.py", line 126, in connect    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  remaining connection slots are reserved for non-replication superuser connections

According to the Postgres documentation, this error occurs when Django makes more connections to the Postgres DB then it's default connection limit.

  • I would like to know what could be the reasons for Django to be initiating/an opening lot of connections to the DB.
  • I would like to know what are the best practices we can do to prevent it from the exception.
  • How to increase the default connection limit for Postgres?
#settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'xxxxx',
        'USER': 'xxxxx',
        'PASSWORD':'xxxxx',
        'HOST':'localhost',
        'PORT':'5432',
    }
like image 347
Arbazz Hussain Avatar asked Nov 07 '22 10:11

Arbazz Hussain


1 Answers

I believe that this is ocuring given Django framework don't make database pooling. Looking into Django docs (https://docs.djangoproject.com/en/3.2/ref/databases/#transaction-pooling-and-server-side-cursors) i could find a sugestion to user pgBouncer (http://www.pgbouncer.org/) to deal with that.

like image 75
Thiago Schettini Avatar answered Nov 12 '22 11:11

Thiago Schettini