Reputation: 153
I am running a Postgresql database on AWS and the backend is Django. After some requests, let's say 50, it raises the error "OperationalError: terminating connection due to administrator command SSL connection has been closed unexpectedly" but the database will still remain active. At first, it was throwing "OperationalError: FATAL: remaining connection slots are reserved for non-replication superuser connections" after some requests, so I have a script that closes open connections. Here is the script:
export PGPASSWORD='mypassword'
psql --host=dbhost.myregion.rds.amazonaws.com --port=5432 --username=user --dbname=name \
-c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
AND state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled')
AND usename != 'rdsadmin';"
but the errors keep coming. I have also tried increasing the max_connections
to 150, but it still doesn't help. I have also tried using AWS RDS proxy, but still no hope.
Here is how I connect to the DB from django:
DATABASES = {
'default': {
'ENGINE': config('DB_ENGINE'),
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
'CONN_MAX_AGE': 0
}
}
Upvotes: 0
Views: 648
Reputation: 246013
The errors you see are caused by your script that closes your connections.
Rather than randomly killing sessions, fix the connection leak in your application (and don't ask us how to do that – we cannot debug your program).
Upvotes: 0