Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

32.18. 连接参数的 LDAP 查找 #

如果 libpq 已使用 LDAP 支持(configure 的选项 --with-ldap)进行编译,则可以从中心服务器通过 LDAP 检索连接选项,例如 hostdbname。优势在于,如果数据库的连接参数发生变化,则无需在所有客户端计算机上更新连接信息。

LDAP 连接参数查找使用连接服务文件 pg_service.conf(请参见 第 32.17 节)。从 ldap:// 开始的 pg_service.conf 段中的一行将被识别为 LDAP URL,并会执行 LDAP 查询。结果必须为 关键字 = 值 对列表,该列表将用于设置连接选项。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=*)