postgres_pass

The `postgres_pass` directive sets the upstream server name for handling database connections in NGINX's PostgreSQL module.

Syntaxpostgres_pass upstream;
Defaultnone
Contextlocation, if in location
Arguments1

Description

The postgres_pass directive is an essential component of the NGINX PostgreSQL module that allows NGINX to forward requests to specified upstream PostgreSQL servers. This directive is utilized in the location and if in location contexts, and it helps determine which backend should handle the incoming database queries. The argument for this directive should be the name of an upstream block that has been defined using the postgres_server directive, which contains details like server IP, database name, user credentials, and connection settings. This enables seamless routing of database requests to the correct database server based on the application needs.

When configuring the postgres_pass directive, it can accept a variable name as its argument, giving flexibility in determining the upstream server dynamically. This could be particularly useful in scenarios where the application needs to interact with different databases based on the content of the request or other runtime factors. By linking the request-processing logic with the upstream definition, NGINX can efficiently manage database interactions, ensuring proper load distribution and failover capabilities among multiple PostgreSQL servers, if configured. Additionally, since it operates in a specific context, you can combine it with other directives like postgres_query to create more sophisticated database interaction patterns.

Config Example

location /database {
    postgres_pass backend_db;
    postgres_query SELECT * FROM users WHERE id = $arg_id;
}

Ensure the specified upstream block exists; otherwise, NGINX will not be able to forward requests and will return an error.

When using variables in the postgres_pass, ensure they resolve to valid upstream names to avoid runtime errors.

Do not place postgres_pass outside of the defined context; it is only valid within location or if in location blocks.

← Back to all directives