2025年9月25日: PostgreSQL 18 发布!
支持的版本: 当前 (18) / 17 / 16 / 15 / 14 / 13
开发版本: devel
不支持的版本: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2

32.18. LDAP 查询连接参数 #

如果 libpq 是通过 LDAP 支持编译的(对于 configure 使用 --with-ldap 选项),就可以通过 LDAP 从中央服务器检索连接选项,例如 hostdbname。其优点是,如果数据库的连接参数发生变化,就不必在所有客户端机器上更新连接信息。

LDAP 连接参数查询使用连接服务文件 pg_service.conf(参见 第 32.17 节)。pg_service.conf 节中的一个以 ldap:// 开头的行将被识别为 LDAP URL,并执行 LDAP 查询。结果必须是 keyword = value 对的列表,这些对将用于设置连接选项。URL 必须符合 RFC 1959,形式如下:

ldap://[hostname[:port]]/search_base?attribute?search_scope?filter

其中 hostname 默认为 localhostport 默认为 389。

在成功进行 LDAP 查询后,pg_service.conf 的处理将终止,但在无法联系 LDAP 服务器时会继续处理。这是为了提供一个回退机制,可以继续处理指向不同 LDAP 服务器的 LDAP URL 行、传统的 keyword = value 对,或默认的连接选项。如果你希望在这种情况下收到错误消息,可以在 LDAP URL 之后添加一个语法错误的行。

使用 LDIF 文件创建的示例 LDAP 条目

version:1
dn:cn=mydatabase,dc=mycompany,dc=com
changetype:add
objectclass:top
objectclass:device
cn:mydatabase
description:host=dbserver.mycompany.com
description:port=5439
description:dbname=mydb
description:user=mydb_user
description:sslmode=require

可以使用以下 LDAP URL 进行查询

ldap://ldap.mycompany.com/dc=mycompany,dc=com?description?one?(cn=mydatabase)

您也可以将常规的服务文件条目与 LDAP 查询混合使用。pg_service.conf 中某个节的完整示例将是

# only host and port are stored in LDAP, specify dbname and user explicitly
[customerdb]
dbname=customer
user=appuser
ldap://ldap.acme.com/cn=dbserver,cn=hosts?pgconnectinfo?base?(objectclass=*)

提交更正

如果您在文档中看到任何不正确、与您对特定功能的体验不符或需要进一步说明的内容,请使用 此表单 报告文档问题。