September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

1.10. Environment Variables

The following environment variables can be used to select default connection parameter values, which will be used by PQconnectdb, PQsetdbLogin and PQsetdb if no value is directly specified by the calling code. These are useful to avoid hard-coding database connection information into simple client applications.

  • PGHOST sets the default server name. If this begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored (default /tmp).

  • PGPORT sets the default TCP port number or Unix-domain socket file extension for communicating with the PostgreSQL backend.

  • PGDATABASE sets the default PostgreSQL database name.

  • PGUSER sets the user name used to connect to the database and for authentication.

  • PGPASSWORD sets the password used if the backend demands password authentication. This functionality is deprecated for security reasons; consider migrating to use the $HOME/.pgpass file.

  • PGREALM sets the Kerberos realm to use with PostgreSQL, if it is different from the local realm. If PGREALM is set, PostgreSQL applications will attempt authentication with servers for this realm and use separate ticket files to avoid conflicts with local ticket files. This environment variable is only used if Kerberos authentication is selected by the backend.

  • PGOPTIONS sets additional run-time options for the PostgreSQL backend.

  • PGTTY sets the file or tty on which debugging messages from the backend server are displayed.

  • PGREQUIRESSL sets whether or not the connection must be made over SSL. If set to "1", libpq will refuse to connect if the server does not accept an SSL connection. This option is only available if PostgreSQL is compiled with SSL support.

  • PGCONNECT_TIMEOUT sets the maximum number of seconds that libpq will wait when attempting to connect to the PostgreSQL server. This option should be set to at least 2 seconds.

The following environment variables can be used to specify user-level default behavior for every PostgreSQL session:

  • PGDATESTYLE sets the default style of date/time representation.

  • PGTZ sets the default time zone.

  • PGCLIENTENCODING sets the default client encoding (if multibyte support was selected when configuring PostgreSQL).

The following environment variables can be used to specify default internal behavior for every PostgreSQL session:

  • PGGEQO sets the default mode for the genetic optimizer.

Refer to the SET SQL command for information on correct values for these environment variables.