london_utku
london_utku

Reputation: 1312

Google Cloud App Engine - Django App using PostGreSQL loses SQL connection

I have been using PostGreSQL via Google Cloud App Engine for my Django Application. Lately, I have started seeing following issue :

...
File "/env/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner      return func(*args, **kwargs)    
File "/env/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection      self.connect()    
File "/env/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__      raise dj_exc_value.with_traceback(traceback) from exc_value    
File "/env/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection      self.connect()    
File "/env/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner      return func(*args, **kwargs)    
File "/env/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect      self.connection = self.get_new_connection(conn_params)    
File "/env/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner      return func(*args, **kwargs)    
File "/env/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection      connection = Database.connect(**conn_params)    
File "/env/lib/python3.7/site-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

My database settings are as follow :

if os.getenv('GAE_APPLICATION', None):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'HOST': '/cloudsql/MYAPP:europe-west2:MYAPP',
            'NAME': 'MYNAME',
            'USER': 'MYUSER',
            'PASSWORD': access_secret_version("MYSECRET_SQL_PASSWORD"),
            'PORT': '5432',
        }
    }

What does this actually mean ?

Upvotes: 0

Views: 303

Answers (1)

A.Amiri
A.Amiri

Reputation: 87

This basically means that you are reaching the concurrent connection limit to your instance and need to either increase the limit or close some connections that are not needed.

You can increase the connection limit by using the max_connections flag to configure it, however, it is recommended to set a limit that keeps the instance stable. You can also change your instance machine type whith higher memory as it offers a higher default concurrent connection limit.

Another solution would be to use db connection pooling with pgbouncer, as Django framework does not offer database connection pooling. In addition, setting CONN_MAX_AGE to zero would help in some cases, as it allows closing the database connection at the end of each request.

Upvotes: 1

Related Questions