databases
databases copied to clipboard
asyncpg.exceptions.TooManyConnectionsError: too many connections for role
Hi,
I'm trying out databases with FastAPI.
I've set up the database connection management on startup and shutdown, just like how it's shown in the documentation.
I'm also saving initialized global database and use them in all endpoints for all requests.
However, when I start testing things out with the frontend on the cloud, I'm getting the too many connections for role issue.
asyncpg.exceptions.TooManyConnectionsError: too many connections for role "vslyvkeneoptxy"
Any suggestion on how I'm supposed to use database? Do I need to start managing the connections..? Is this production ready?
I think it would require more information like example of how you're making queries, code samples, etc to help with it.
I'm having the same issue in the same scenario. Starting gunicorn with uvicorn workers. But the behaviour seems unpredictable, some times it works with 6 workers, sometimes it don't with 2.
Command is:
gunicorn app.main:app --workers 2 -k uvicorn.workers.UvicornWorker --bind unix:category_tags.sock --error-logfile /home/xpuser/category-tags/log/errors.log
Do you have any news about this?
You need to pass a ‘max_size’ kwarg to ‘Database’ to limit the connection pool per worker. This should be less than the total allowed connections divided by number of workers.
You need to pass a ‘max_size’ kwarg to ‘Database’ to limit the connection pool per worker. This should be less than the total allowed connections divided by number of workers.
Yes it's what i did, something like:
database = databases.Database(DATABASE_URL + f'?min_size={DATABASE_MIN_POOL_SIZE}&max_size={DATABASE_MAX_POOL_SIZE}')