Good technique for connections with PostgreSQL Good technique for connections with PostgreSQL postgresql postgresql

Good technique for connections with PostgreSQL


PostgreSQL supports connections pooling (pool size is customizable) so the common pattern:

using (NpgsqlConnection conn = new NpgsqlConnection(...)){...}

should be the better choice.


In my opinion you should open a connection at the moment you need it, and close it right after it. This will prevent a lot of connections on the server to be kept alive.

In my experience, opening a connection doesn't take that much time (a few milliseconds, usually a fraction of your execution time), so you don't have to worry too much.