postgres_connect_timeout
The `postgres_connect_timeout` directive sets the maximum time to wait for a connection to a PostgreSQL database before timing out.
Description
The postgres_connect_timeout directive is used in the PostgreSQL module of NGINX to define a timeout period for establishing a connection to a PostgreSQL database server. Specified in milliseconds, the directive allows administrators to avoid indefinite waiting during connection attempts, which could potentially lead to unresponsive behavior in their web applications. If the connection is not established within the specified timeout, NGINX will log an error and return a failure response to the client, freeing up resources that would otherwise be tied up during the connection attempt.
This directive can be set in various contexts including http, server, or location. The flexibility of placement allows fine-grained control over outgoing connection timeouts to different database backend servers based on the respective contexts. For users of NGINX, especially in high-demand environments or those that experience intermittent networking issues, tuning this value can yield significant performance benefits and enhance the reliability of database interactions. If not set, the connection will indefinitely wait, which can lead to delays and poor user experience under certain conditions.
Config Example
location /api/data {
postgres_pass my_postgres;
postgres_connect_timeout 5000;
postgres_query SELECT * FROM my_table;
}Setting a too-short timeout might lead to frequent connection errors, especially on slow networks.
The timeout value is in milliseconds; ensure to specify the value accordingly to avoid confusion.