Postgresql 中文操作指南

34.17. The Connection Service File #

连接服务文件允许将 libpq 连接参数与一个服务名称相关联。然后,可以在 libpq 连接字符串中指定服务名称,并将使用关联的设置。这样可以在不需要重新编译使用 libpq 的应用程序的情况下修改连接参数。还可以使用 PGSERVICE 环境变量指定服务名称。

The connection service file allows libpq connection parameters to be associated with a single service name. That service name can then be specified in a libpq connection string, and the associated settings will be used. This allows connection parameters to be modified without requiring a recompile of the libpq-using application. The service name can also be specified using the PGSERVICE environment variable.

可以在每个用户服务文件或系统范围文件内定义服务名称。如果相同的服务名称同时存在于用户文件和系统文件中,则用户文件具有优先权。默认情况下,每个用户服务文件被命名为 ~/.pg_service.conf。在 Microsoft Windows 上,它被命名为 %APPDATA%\postgresql\.pg_service.conf(其中 %APPDATA% 是指用户配置文件中的 Application Data 子目录)。可以通过设置环境变量 PGSERVICEFILE 指定不同的文件名称。系统范围文件被命名为 pg_service.conf。默认情况下,它在 PostgreSQL 安装的 etc 目录中进行查找(使用 pg_config --sysconfdir 准确识别此目录)。可以通过设置环境变量 PGSYSCONFDIR 指定另一个目录,但不能指定不同的文件名称。

Service names can be defined in either a per-user service file or a system-wide file. If the same service name exists in both the user and the system file, the user file takes precedence. By default, the per-user service file is named ~/.pg_service.conf. On Microsoft Windows, it is named %APPDATA%\postgresql\.pg_service.conf (where %APPDATA% refers to the Application Data subdirectory in the user’s profile). A different file name can be specified by setting the environment variable PGSERVICEFILE. The system-wide file is named pg_service.conf. By default it is sought in the etc directory of the PostgreSQL installation (use pg_config --sysconfdir to identify this directory precisely). Another directory, but not a different file name, can be specified by setting the environment variable PGSYSCONFDIR.

两个服务文件都使用“INI 文件”格式,其中部分名称是服务名称,参数是连接参数;有关列表,请参阅 Section 34.1.2。例如:

Either service file uses an “INI file” format where the section name is the service name and the parameters are connection parameters; see Section 34.1.2 for a list. For example:

# comment
[mydb]
host=somehost
port=5433
user=admin

share/pg_service.conf.sample 的 PostgreSQL 安装中提供了示例文件。

An example file is provided in the PostgreSQL installation at share/pg_service.conf.sample.

从服务文件获取的连接参数与从其他来源获取的参数相结合。服务文件设置将覆盖相应的环境变量,并且反过来还可以通过在连接字符串中直接给出的值将其覆盖。例如,使用上述服务文件,连接字符串 service=mydb port=5434 将使用由环境变量或内置默认值设置的主机 somehost、端口 5434、用户 admin 和其他参数。

Connection parameters obtained from a service file are combined with parameters obtained from other sources. A service file setting overrides the corresponding environment variable, and in turn can be overridden by a value given directly in the connection string. For example, using the above service file, a connection string service=mydb port=5434 will use host somehost, port 5434, user admin, and other parameters as set by environment variables or built-in defaults.