34.17. LDAP 查找连接参数

如果 libpq 已使用 LDAP 支持进行编译(configure的选项--with-ldap),则可以从中央服务器通过 LDAP 检索hostdbname之类的连接选项。优点是,如果数据库的连接参数发生更改,则不必在所有 Client 端计算机上都更新连接信息。

LDAP 连接参数查找使用连接服务文件pg_service.conf(请参阅Section 34.16)。 pg_service.conf节中以ldap://开头的行将被识别为 LDAP URL,并且将执行 LDAP 查询。结果必须是keyword = value对的列表,这些列表将用于设置连接选项。该 URL 必须符合 RFC 1959,格式为

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

其中* hostname 默认为localhost port *默认为 389.

LDAP 查找成功后,pg_service.conf的处理终止,但如果无法联系 LDAP 服务器,则 continue 处理pg_service.conf。这是为了提供备用的 LDAP URL,以提供更多指向其他 LDAP 服务器,经典keyword = value对或默认连接选项的 LDAP URL 行。如果您希望在这种情况下收到错误消息,请在 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=*)