databases icon indicating copy to clipboard operation
databases copied to clipboard

asyncpg.exceptions.TooManyConnectionsError: too many connections for role

Open jocho-here opened this issue 3 years ago • 4 comments

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?

jocho-here avatar Jan 05 '22 11:01 jocho-here

I think it would require more information like example of how you're making queries, code samples, etc to help with it.

aminalaee avatar Jan 07 '22 13:01 aminalaee

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?

sergiobrr avatar Apr 28 '22 13:04 sergiobrr

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.

josheppinette avatar Apr 28 '22 13:04 josheppinette

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}')

sergiobrr avatar May 26 '22 08:05 sergiobrr