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. 使用使用者名稱和密碼從遠端登入伺服器。