> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/client-interfaces/libpq/libpq-envars.md).

# 32.15. Environment Variables \#

The following environment variables can be used to select default connection parameter values, which will be used by [`PQconnectdb`](/client-interfaces/libpq/libpq-connect.md#LIBPQ-PQCONNECTDB), [`PQsetdbLogin`](/client-interfaces/libpq/libpq-connect.md#LIBPQ-PQSETDBLOGIN) and [`PQsetdb`](/client-interfaces/libpq/libpq-connect.md#LIBPQ-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, for example.

* `PGHOST` behaves the same as the [host](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-HOST) connection parameter.
* `PGSSLNEGOTIATION` behaves the same as the [sslnegotiation](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLNEGOTIATION) connection parameter.
* `PGHOSTADDR` behaves the same as the [hostaddr](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-HOSTADDR) connection parameter. This can be set instead of or in addition to `PGHOST` to avoid DNS lookup overhead.
* `PGPORT` behaves the same as the [port](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-PORT) connection parameter.
* `PGDATABASE` behaves the same as the [dbname](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-DBNAME) connection parameter.
* `PGUSER` behaves the same as the [user](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-USER) connection parameter.
* `PGPASSWORD` behaves the same as the [password](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-PASSWORD) connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using a password file (see [Section 32.16](/client-interfaces/libpq/libpq-pgpass.md)).
* `PGPASSFILE` behaves the same as the [passfile](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-PASSFILE) connection parameter.
* `PGREQUIREAUTH` behaves the same as the [require\_auth](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-REQUIRE-AUTH) connection parameter.
* `PGCHANNELBINDING` behaves the same as the [channel\_binding](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-CHANNEL-BINDING) connection parameter.
* `PGSERVICE` behaves the same as the [service](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SERVICE) connection parameter.
* `PGSERVICEFILE` specifies the name of the per-user connection service file (see [Section 32.17](/client-interfaces/libpq/libpq-pgservice.md)). Defaults to `~/.pg_service.conf`, or `%APPDATA%\postgresql\.pg_service.conf` on Microsoft Windows.
* `PGOPTIONS` behaves the same as the [options](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-OPTIONS) connection parameter.
* `PGAPPNAME` behaves the same as the [application\_name](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-APPLICATION-NAME) connection parameter.
* `PGSSLMODE` behaves the same as the [sslmode](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLMODE) connection parameter.
* `PGREQUIRESSL` behaves the same as the [requiressl](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-REQUIRESSL) connection parameter. This environment variable is deprecated in favor of the `PGSSLMODE` variable; setting both variables suppresses the effect of this one.
* `PGSSLCOMPRESSION` behaves the same as the [sslcompression](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLCOMPRESSION) connection parameter.
* `PGSSLCERT` behaves the same as the [sslcert](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLCERT) connection parameter.
* `PGSSLKEY` behaves the same as the [sslkey](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLKEY) connection parameter.
* `PGSSLCERTMODE` behaves the same as the [sslcertmode](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLCERTMODE) connection parameter.
* `PGSSLROOTCERT` behaves the same as the [sslrootcert](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLROOTCERT) connection parameter.
* `PGSSLCRL` behaves the same as the [sslcrl](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLCRL) connection parameter.
* `PGSSLCRLDIR` behaves the same as the [sslcrldir](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLCRLDIR) connection parameter.
* `PGSSLSNI` behaves the same as the [sslsni](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSLSNI) connection parameter.
* `PGREQUIREPEER` behaves the same as the [requirepeer](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-REQUIREPEER) connection parameter.
* `PGSSLMINPROTOCOLVERSION` behaves the same as the [ssl\_min\_protocol\_version](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSL-MIN-PROTOCOL-VERSION) connection parameter.
* `PGSSLMAXPROTOCOLVERSION` behaves the same as the [ssl\_max\_protocol\_version](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-SSL-MAX-PROTOCOL-VERSION) connection parameter.
* `PGGSSENCMODE` behaves the same as the [gssencmode](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-GSSENCMODE) connection parameter.
* `PGKRBSRVNAME` behaves the same as the [krbsrvname](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-KRBSRVNAME) connection parameter.
* `PGGSSLIB` behaves the same as the [gsslib](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-GSSLIB) connection parameter.
* `PGGSSDELEGATION` behaves the same as the [gssdelegation](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-GSSDELEGATION) connection parameter.
* `PGCONNECT_TIMEOUT` behaves the same as the [connect\_timeout](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-CONNECT-TIMEOUT) connection parameter.
* `PGCLIENTENCODING` behaves the same as the [client\_encoding](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-CLIENT-ENCODING) connection parameter.
* `PGTARGETSESSIONATTRS` behaves the same as the [target\_session\_attrs](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-TARGET-SESSION-ATTRS) connection parameter.
* `PGLOADBALANCEHOSTS` behaves the same as the [load\_balance\_hosts](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-LOAD-BALANCE-HOSTS) connection parameter.
* `PGMINPROTOCOLVERSION` behaves the same as the [min\_protocol\_version](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-MIN-PROTOCOL-VERSION) connection parameter.
* `PGMAXPROTOCOLVERSION` behaves the same as the [max\_protocol\_version](/client-interfaces/libpq/libpq-connect.md#LIBPQ-CONNECT-MAX-PROTOCOL-VERSION) connection parameter.

The following environment variables can be used to specify default behavior for each PostgreSQL session. (See also the [ALTER ROLE](/reference/sql-commands/sql-alterrole.md) and [ALTER DATABASE](/reference/sql-commands/sql-alterdatabase.md) commands for ways to set default behavior on a per-user or per-database basis.)

* `PGDATESTYLE` sets the default style of date/time representation. (Equivalent to `SET datestyle TO ...`.)
* `PGTZ` sets the default time zone. (Equivalent to `SET timezone TO ...`.)
* `PGGEQO` sets the default mode for the genetic query optimizer. (Equivalent to `SET geqo TO ...`.)

Refer to the SQL command [SET](/reference/sql-commands/sql-set.md) for information on correct values for these environment variables.

The following environment variables determine internal behavior of libpq; they override compiled-in defaults.

* `PGSYSCONFDIR` sets the directory containing the `pg_service.conf` file and in a future version possibly other system-wide configuration files.
* `PGLOCALEDIR` sets the directory containing the `locale` files for message localization.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/libpq-envars.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/client-interfaces/libpq/libpq-envars.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
