53300: sorry, too many clients already
Randomly started getting this error when running tests using testcontainers...
Npgsql.PostgresException: '53300: sorry, too many clients already'
Always the same test would fail - initially just returning a System.NullReferenceException
with no indication of what was happening. After a bit of digging around found that the underlying problem was using up all the available connections.
After a bit more digging around found that there's a command line parameter you can use to increase the maximum number of connection.
So updated the test container from this...
private readonly PostgreSqlContainer _postgresSqlContainer = new PostgreSqlBuilder()
.WithImage("postgres:16")
.Build();
To this...
private readonly PostgreSqlContainer _postgresSqlContainer = new PostgreSqlBuilder()
.WithImage("postgres:16")
.WithEntrypoint("docker-entrypoint.sh")
.WithCommand("-c", "max_connections=20000", "-c", "shared_buffers=256MB")
.Build();
and no more errors (for now!)