如果libpq已经在编译时打开了 LDAP 支持(configure的选项),就可以通过 LDAP 从一个中央服务器检索--with-ldaphost或dbname之类的连接参数。这样做的好处是如果一个数据库的连接参数改变,不需要在所有的客户端机器上更新连接信息。
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 服务器,则会继续处理。这使后续指向其他 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中一节的完整示例:
# 只有主机和端口存储在LDAP中,显式指定dbname和user。 [customerdb] dbname=customer user=appuser ldap://ldap.acme.com/cn=dbserver,cn=hosts?pgconnectinfo?base?(objectclass=*)