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

快速入门:使用 ARM 模板通过 Azure Database for PostgreSQL 创建弹性群集

使用弹性群集的 Azure Database for PostgreSQL 是一种托管服务,可用于在云中运行、管理和缩放高度可用的 PostgreSQL 数据库,并提供横向扩展功能。 可以使用 Azure 资源管理器模板(ARM 模板)创建弹性群集实例。

Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

Azure 资源管理器是 Azure 的部署和管理服务。 它提供了一个管理层,用于在 Azure 帐户中创建、更新和删除资源。 部署后,可以使用访问控制、锁和标记等管理功能来保护和组织资源。 若要了解 Azure 资源管理器模板,请参阅模板部署概述

Prerequisites

具有活动订阅的 Azure 帐户。 免费创建一个

查看模板

Azure Database for PostgreSQL 灵活服务器实例是某区域中某个分布式数据库的父资源。 它提供应用于群集的管理策略的作用域:防火墙、用户、角色和配置。

创建 elastic-cluster-template.json 文件,并将以下 JSON 脚本复制到其中。

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "administratorLogin": {
      "type": "string"
    },
    "administratorLoginPassword": {
      "type": "securestring"
    },
    "location": {
      "type": "string",
      "defaultValue": "canadacentral"
    },
    "clusterName": {
      "type": "string"
    },
    "serverEdition": {
      "type": "string",
      "defaultValue": "GeneralPurpose"
    },
    "storageSizeGB": {
      "type": "int",
      "defaultValue": 64
    },
    "haEnabled": {
      "type": "string",
      "defaultValue": "Disabled"
    },
    "availabilityZone": {
      "type": "string",
      "defaultValue": "1"
    },
    "backupRetentionDays": {
      "type": "int",
      "defaultValue": 7
    },
    "skuName": {
      "type": "string",
      "defaultValue": "Standard_D4ds_v5"
    },
    "clusterSize": {
      "type": "int",
      "defaultValue": 2
    },
    "guid": {
      "type": "string",
      "defaultValue": "[newGuid()]"
    },
    "firewallRules": {
      "type": "object",
      "defaultValue": {
        "rules": [
          {
            "name": "AllowAll",
            "startIPAddress": "0.0.0.0",
            "endIPAddress": "255.255.255.255"
          }
        ]
      }
    },
    "network": {
      "type": "object",
      "defaultValue": { "publicNetworkAccess": "Enabled" }
    }
  },
  "variables": {
    "firewallRules": "[parameters('firewallRules').rules]"
  },
  "resources": [
    {
      "apiVersion": "2025-08-01",
      "location": "[parameters('location')]",
      "name": "[parameters('clusterName')]",
      "properties": {
        "createMode": "Default",
        "version": "16",
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "availabilityZone": "[parameters('availabilityZone')]",
        "Storage": {
          "StorageSizeGB": "[parameters('storageSizeGB')]",
          "Autogrow": "Disabled"
        },
        "Network": "[if(empty(parameters('network')), json('null'), parameters('network'))]",
        "Backup": {
          "backupRetentionDays": "[parameters('backupRetentionDays')]",
          "geoRedundantBackup": "Disabled"
        },
        "highAvailability": {
          "mode": "[parameters('haEnabled')]"
        },
        "cluster": {
          "clusterSize": "[parameters('clusterSize')]"
        }
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "tier": "[parameters('serverEdition')]"
      },
      "type": "Microsoft.DBforPostgreSQL/flexibleServers"
    },
    {
      "condition": "[greater(length(variables('firewallRules')), 0)]",
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2019-08-01",
      "name": "[concat('firewallRules-', parameters('guid'), '-', copyIndex())]",
      "copy": {
        "count": "[if(greater(length(variables('firewallRules')), 0), length(variables('firewallRules')), 1)]",
        "mode": "Serial",
        "name": "firewallRulesIterator"
      },
      "dependsOn": [
        "[concat('Microsoft.DBforPostgreSQL/flexibleServers/', parameters('clusterName'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.DBforPostgreSQL/flexibleServers/firewallRules",
              "name": "[concat(parameters('clusterName'),'/',variables('firewallRules')[copyIndex()].name)]",
              "apiVersion": "2025-08-01",
              "properties": {
                "StartIpAddress": "[variables('firewallRules')[copyIndex()].startIPAddress]",
                "EndIpAddress": "[variables('firewallRules')[copyIndex()].endIPAddress]"
              }
            }
          ]
        }
      }
    }
  ]
}

该模板中定义了以下资源:

部署模板

从以下 PowerShell 代码块中选择“试用”以打开 Azure Cloud Shell。

$clusterName = Read-Host -Prompt "Enter a name for the new Azure Database for PostgreSQL flexible server elastic cluster instance"
$resourceGroupName = Read-Host -Prompt "Enter a name for the new resource group where the elastic cluster will exist"
$adminUser = Read-Host -Prompt "Enter the Azure Database for PostgreSQL flexible server elastic cluster instance's administrator account name"
$adminPassword = Read-Host -Prompt "Enter the administrator password" -AsSecureString
# New-AzResourceGroup -Name $resourceGroupName -Location <AZURE_LOCATION>  Use this command when you need to create a new resource group for your deployment
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
    -TemplateFile "elastic-cluster-template.json" `
    -clusterName $clusterName `
    -administratorLogin $adminUser `
    -administratorLoginPassword $adminPassword

Read-Host -Prompt "Press [ENTER] to continue ..."

查看已部署的资源

按照以下步骤验证是否已创建 Azure Database for PostgreSQL 灵活服务器弹性群集。

  1. Azure 门户中,搜索并选择“Azure Database for PostgreSQL 灵活服务器”
  2. 在数据库列表中,选择新服务器以查看“ 概述 ”页来管理弹性群集。

后续步骤

若要继续使用此资源组和弹性群集,请继续执行 “相关内容 ”部分中列出的后续建议步骤。 后续步骤演示如何对不同的应用程序分片模型和设计使用弹性群集。

清理资源

完成弹性群集环境后,可以删除弹性群集资源:

若要删除弹性群集,请执行以下作:

门户中,选择要删除的资源组。

  1. 选择“删除资源组”
  2. 若要确认删除,请键入资源组的名称。