postgres_server

The 'postgres_server' directive defines the details of a PostgreSQL database server for NGINX to connect to.

Syntaxpostgres_server ip[:port] dbname=dbname user=user password=pass;
Defaultnone
Contextupstream
Arguments1+

Description

The 'postgres_server' directive is part of the NGINX PostgreSQL module and is used within an 'upstream' context to specify the connection parameters for a PostgreSQL server. It accepts one or more arguments, where each argument is an IP address (and optional port), along with parameters that define the database name, user, and password for authentication. This allows NGINX to handle requests to the database efficiently, drawing on the configuration specified for load distribution and connection settings.

Parameters can be defined in the format: ip[:port] dbname=dbname user=user password=pass. For example, 192.168.1.100:5432 dbname=mydatabase user=admin password=secret specifies a server running PostgreSQL at the given IP address and port, connected to a database called 'mydatabase' with the given credentials. Each upstream block can contain multiple 'postgres_server' directives to define failover servers or clusters, facilitating high availability and load balancing in database-driven applications.

The implementation ensures that as requests come in from clients, NGINX can efficiently route those to the designated PostgreSQL server as defined in the upstream configuration. This directive is essential for setting up a functioning upstream service that allows NGINX to interact directly with PostgreSQL databases, enhancing performance and scalability for web applications.

Config Example

upstream postgres_backend {
    postgres_server 192.168.1.100:5432 dbname=mydatabase user=admin password=secret;
    postgres_server 192.168.1.101:5432 dbname=mydatabase user=admin password=secret;
}

Ensure that the PostgreSQL server is accessible from the NGINX server; otherwise, connection attempts will fail.

Double-check that the correct IP address and port are specified, as incorrect settings will prevent database connectivity.

User permissions in PostgreSQL must align with the specified user in the directive for effective access.

← Back to all directives