hostaddr
要连接主机的数值 IP 地址,应使用标准 IPv4 格式,例如 172.28.40.9。机器支持 IPv6 时,也可以使用 IPv6 地址。只要此参数为非空字符串,就始终使用 TCP/IP 通信。未指定此参数时,会解析 host 以取得对应 IP 地址;如果 host 本身是 IP 地址,则直接使用。
当前查看 PostgreSQL 18.6。
说明
要连接主机的数值 IP 地址,应使用标准 IPv4 格式,例如 172.28.40.9。机器支持 IPv6 时,也可以使用 IPv6 地址。只要此参数为非空字符串,就始终使用 TCP/IP 通信。未指定此参数时,会解析 host 以取得对应 IP 地址;如果 host 本身是 IP 地址,则直接使用。
- 客户端库
- libpq 18.6
- 手册定义
- 手册已记载
- 源码中的环境变量回退
- PGHOSTADDR
- 编译时回退表达式
- NULL
用法
hostaddr默认值解析与服务文件优先级
以下环境变量可用于选择连接参数的默认值,供 PQconnectdb 、 PQsetdbLogin 和 PQsetdb 在调用代码未直接指定参数值时使用。例如,这样可以避免在简单的客户端应用程序中硬编码数据库连接信息。
服务名称可以在每个用户的服务文件或系统范围的文件中定义。如果同一个服务名称存在于用户文件和系统文件中,则用户文件优先。默认情况下,每个用户的服务文件名为 ~/.pg_service.conf 。在Microsoft Windows上,它的名称为 %APPDATA%\postgresql\.pg_service.conf (其中 %APPDATA% 指用户配置文件夹中的应用数据子目录)。可以通过设置环境变量 PGSERVICEFILE 来指定不同的文件名。系统范围的文件名为 pg_service.conf 。默认情况下,在 PostgreSQL 安装的 etc 目录中寻找(使用 pg_config --sysconfdir 来准确识别此目录)。可以通过设置环境变量 PGSYSCONFDIR 来指定另一个目录,但不能指定不同的文件名。
从服务文件中获取的连接参数会与其他来源的参数合并。服务文件中的设置会覆盖相应的环境变量,而连接字符串中直接给出的值又会覆盖服务文件中的设置。例如,使用上述服务文件时,连接字符串 service=mydb port=5434 将使用主机 somehost 、端口 5434 、用户 admin ,以及由环境变量或内置默认值设置的其他参数。
环境变量证据
PGHOSTADDR 的行为与 hostaddr 连接参数相同。这可以替代或者与 PGHOST 一起设置,以避免DNS查找开销。
环境变量回退
| 变量 | 手册记载的行为 |
|---|---|
| PGHOSTADDR | PGHOSTADDR 的行为与 hostaddr 连接参数相同。这可以替代或者与 PGHOST 一起设置,以避免DNS查找开销。 |
手册定义
hostaddr-
要连接主机的数值 IP 地址,应使用标准 IPv4 格式,例如 172.28.40.9。机器支持 IPv6 时,也可以使用 IPv6 地址。只要此参数为非空字符串,就始终使用 TCP/IP 通信。未指定此参数时,会解析 host 以取得对应 IP 地址;如果 host 本身是 IP 地址,则直接使用。
使用 hostaddr 可避免主机名解析,这对有时间限制的应用可能很重要。不过,GSSAPI 或 SSPI 认证,以及 verify-full 级别的 SSL 证书验证都需要主机名。具体规则如下:
- 指定 host 而未指定 hostaddr 时,会执行主机名解析。使用 PQconnectPoll 时,解析发生在其首次处理此主机名时,可能使 PQconnectPoll 阻塞相当长的时间。
- 指定 hostaddr 而未指定 host 时,hostaddr 提供服务器网络地址。如果认证方法要求主机名,连接尝试将失败。
- 同时指定 host 和 hostaddr 时,hostaddr 提供服务器网络地址。除非认证方法需要主机名,否则忽略 host;需要时使用 host 作为主机名。
请注意,如果
host不是网络地址hostaddr上服务器的名称,则认证可能会失败。此外,当同时指定host和hostaddr时,host用于在密码文件中标识连接(请参阅第 32.16 节)。也可以接受一个逗号分隔的
hostaddr值列表,此时将按顺序尝试列表中的每个主机。列表中的空项会导致使用相应的主机名,如果主机名也为空,则使用默认主机名。详见第 32.1.1.3 节。如果既没有主机名也没有主机地址,libpq 会使用本地 Unix 域套接字连接;在 Windows 上,则会尝试连接到
localhost。
相关条目
文档与源码
- 18.6 English manual · libpq-connect.html
- 18.6 libpq connection option declarations
- 18.6 English manual · libpq-envars.html
- 18.6 English manual · libpq-pgservice.html
来源构建
- 版本
- 18.6
- 构建
- https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
- 来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8
版本比较
PostgreSQL 10 → 11: 属性变化。
以下差异保留原始字段名与英文源描述。
--- PostgreSQL 10
+++ PostgreSQL 11
@@ -1,9 +1,9 @@
{
"compiled_default_expression": "NULL",
"default_evidence": [
- "A comma-separated list of hostaddr values is also accepted, in which case each host in the list is tried in order. An empty item in the list causes the corresponding host name to be used, or the default host name if that is empty as well. See Section 33.1.1.3 for details."
+ "A comma-separated list of hostaddr values is also accepted, in which case each host in the list is tried in order. An empty item in the list causes the corresponding host name to be used, or the default host name if that is empty as well. See Section 34.1.1.3 for details."
],
- "definition": "Numeric IP address of host to connect to. This should be in the standard IPv4 address format, e.g., 172.28.40.9 . If your machine supports IPv6, you can also use those addresses. TCP/IP communication is always used when a nonempty string is specified for this parameter. Using hostaddr instead of host allows the application to avoid a host name look-up, which might be important in applications with time constraints. However, a host name is required for GSSAPI or SSPI authentication methods, as well as for verify-full SSL certificate verification. The following rules are used: If host is specified without hostaddr , a host name lookup occurs. (When using PQconnectPoll , the lookup occurs when PQconnectPoll first considers this host name, and it may cause PQconnectPoll to block for a significant amount of time.) If hostaddr is specified without host , the value for hostaddr gives the server network address. The connection attempt will fail if the authentication method requires a host name. If both host and hostaddr are specified, the value for hostaddr gives the server network address. The value for host is ignored unless the authentication method requires it, in which case it will be used as the host name. Note that authentication is likely to fail if host is not the name of the server at network address hostaddr . Also, when both host and hostaddr are specified, host is used to identify the connection in a password file (see Section 33.15 ). A comma-separated list of hostaddr values is also accepted, in which case each host in the list is tried in order. An empty item in the list causes the corresponding host name to be used, or the default host name if that is empty as well. See Section 33.1.1.3 for details. Without either a host name or host address, libpq will connect using a local Unix-domain socket; or on machines without Unix-domain sockets, it will attempt to connect to localhost .",
+ "definition": "Numeric IP address of host to connect to. This should be in the standard IPv4 address format, e.g., 172.28.40.9 . If your machine supports IPv6, you can also use those addresses. TCP/IP communication is always used when a nonempty string is specified for this parameter. Using hostaddr instead of host allows the application to avoid a host name look-up, which might be important in applications with time constraints. However, a host name is required for GSSAPI or SSPI authentication methods, as well as for verify-full SSL certificate verification. The following rules are used: If host is specified without hostaddr , a host name lookup occurs. (When using PQconnectPoll , the lookup occurs when PQconnectPoll first considers this host name, and it may cause PQconnectPoll to block for a significant amount of time.) If hostaddr is specified without host , the value for hostaddr gives the server network address. The connection attempt will fail if the authentication method requires a host name. If both host and hostaddr are specified, the value for hostaddr gives the server network address. The value for host is ignored unless the authentication method requires it, in which case it will be used as the host name. Note that authentication is likely to fail if host is not the name of the server at network address hostaddr . Also, when both host and hostaddr are specified, host is used to identify the connection in a password file (see Section 34.15 ). A comma-separated list of hostaddr values is also accepted, in which case each host in the list is tried in order. An empty item in the list causes the corresponding host name to be used, or the default host name if that is empty as well. See Section 34.1.1.3 for details. Without either a host name or host address, libpq will connect using a local Unix-domain socket; or on machines without Unix-domain sockets, it will attempt to connect to localhost .",
"documented": true,
"environment": "PGHOSTADDR",
"keyword": "hostaddr"
比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。
相关条目
导出 JSON · 返回连接参数 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。