18.3 连接远程 Remote PostgreSQL 数据库

如果您的 PostgreSQL 数据库安装在单独的服务器上,则需要在该远程数据库的 postgresql.confpg_hba.conf 文件中更改默认设置。

  1. postgresql.conf 文件中更改侦听地址。

    默认情况下,PostgreSQL 允许侦听 localhost 连接,不允许远程 TCP/IP 连接。要允许远程 TCP/IP 连接,请将以下条目添加到 C:\NetIQ\idm\apps\postgres\data\postgresql.conf 文件中:

    listen_addresses = '*'

    如果服务器上有多个接口,可以指定要侦听的特定接口。

  2. 将客户端鉴定条目添加到 pg_hba.conf 文件中。

    默认情况下,PostgreSQL 只接受来自 localhost 的连接。它会拒绝远程连接。这通过应用访问控制规则来控制,该规则允许用户在提供有效口令(md5 关键字)后从某个 IP 地址登录。要接受远程连接,请将以下条目添加到 C:\NetIQ\idm\apps\postgres\data\pg_hba.conf 文件中。

    host all all 0.0.0.0/0 md5

    例如,192.168.104.24/26 trust

    这仅适用于 IPv4 地址。对于 IPv6 地址,请添加以下条目:

    host all all ::0/0 md5

    如果您要允许来自特定网络上多台客户端计算机的连接,请采用 CIDR 地址格式在此条目中指定网络地址。

    pg_hba.conf 文件支持以下客户端鉴定格式。

    • local database user authentication-method [authentication-option]

    • host database user CIDR-address authentication-method [authentication-option]

    • hostssl database user CIDR-address authentication-method [authentication-option]

    • hostnossl database user CIDR-address authentication-method [authentication-option]

    您可以使用以下格式在单独的字段中指定 IP 地址和网络掩码,而不使用 CIDR 地址格式:

    • host database user IP-address IP-mask authentication-method [authentication-option]

    • hostssl database user IP-address IP-mask authentication-method [authentication-option]

    • hostnossl database user IP-address IP-mask authentication-method [authentication-option]

  3. 测试远程连接。

    1. 重启动远程 PostgreSQL 服务器。

    2. 使用用户名和口令远程登录服务器。