你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

获取连接终结点并创建已启用 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 实例的终结点通过 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 实例”的终结点中指示的 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