Share via


為已啟用 Azure Arc 的 PostgreSQL 伺服器取得連線端點及建立連接字串

本文說明如何擷取伺服器群組的連線端點,以及如何建立連接字串,以用於應用程式和/或工具。

注意

作為預覽功能,本文所述的技術受限於 Microsoft Azure 預覽版增補使用規定

版本資訊中提供的最新更新。

取得連線端點:

執行以下命令:

az postgres server-arc endpoint list -n <server name> --k8s-namespace <namespace> --use-k8s

例如:

az postgres server-arc endpoint list -n postgres01 --k8s-namespace arc --use-k8s

傳回端點清單:PostgreSQL 端點、記錄搜尋儀表板 (Kibana),以及計量儀表板 (Grafana)。 例如:

{
  "instances": [
    {
      "endpoints": [
        {
          "description": "PostgreSQL Instance",
          "endpoint": "postgresql://postgres:<replace with password>@12.345.567.89:5432"
        },
        {
          "description": "Log Search Dashboard",
          "endpoint": "https://23.456.78.99:5601/app/kibana#/discover?_a=(query:(language:kuery,query:'custom_resource_name:postgres01'))"
        },
        {
          "description": "Metrics Dashboard",
          "endpoint": "https://34.567.890.12:3000/d/postgres-metrics?var-Namespace=arc&var-Name=postgres01"
        }
      ],
      "engine": "PostgreSql",
      "name": "postgres01"
    }
  ],
  "namespace": "arc"
}

使用這些端點:

  • 建立連接字串,並連線至用戶端工具或應用程式
  • 從瀏覽器存取 Grafana 和 Kibana 儀表板

例如,您可以使用名為 PostgreSQL Instance 的端點,以透過 psql 連線至伺服器群組:

psql postgresql://postgres:MyPassworkd@12.345.567.89:5432
psql (10.14 (Ubuntu 10.14-0ubuntu0.18.04.1), server 12.4 (Ubuntu 12.4-1.pgdg16.04+1))
WARNING: psql major version 10, server major version 12.
         Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#

注意

  • 在名為 "PostgreSQL Instance" 的端點中指出的 postgres 使用者密碼,是您部署伺服器群組時選擇的密碼。

從 CLI 使用 kubectl

kubectl get postgresqls/<server name> -n <namespace name>

例如:

kubectl get postgresqls/postgres01 -n arc

這些命令會產生如下輸出。 您可以使用該資訊來建立連接字串:

NAME         STATE   READY-PODS   PRIMARY-ENDPOINT     AGE
postgres01   Ready   3/3          12.345.567.89:5432   9d

建立連接字串

針對您的伺服器群組,使用下列連接字串範例。 視需要複製、貼上及自訂:

重要

用戶端連線需要 SSL。 在連接字串中,不應該停用 SSL 模式參數。 如需詳細資訊,請檢閱 https://www.postgresql.org/docs/14/runtime-config-connection.html

ADO.NET

Server=192.168.1.121;Database=postgres;Port=24276;User Id=postgres;Password={your_password_here};Ssl Mode=Require;`

C++ (libpq)

host=192.168.1.121 port=24276 dbname=postgres user=postgres password={your_password_here} sslmode=require

JDBC

jdbc:postgresql://192.168.1.121:24276/postgres?user=postgres&password={your_password_here}&sslmode=require

Node.js

host=192.168.1.121 port=24276 dbname=postgres user=postgres password={your_password_here} sslmode=require

PHP

host=192.168.1.121 port=24276 dbname=postgres user=postgres password={your_password_here} sslmode=require

psql

psql "host=192.168.1.121 port=24276 dbname=postgres user=postgres password={your_password_here} sslmode=require"

Python

dbname='postgres' user='postgres' host='192.168.1.121' password='{your_password_here}' port='24276' sslmode='true'

Ruby

host=192.168.1.121; dbname=postgres user=postgres password={your_password_here} port=24276 sslmode=require