共用方式為


快速入門:使用 ARM 範本與 Azure Database for PostgreSQL 建立彈性叢集

Azure Database for PostgreSQL 搭配彈性叢集是一項管理服務,你可以用來在雲端運行、管理及擴展高可用性的 PostgreSQL 資料庫,並具備橫向擴展功能。 您可以使用 Azure Resource Manager 範本 (ARM 樣本) 來建立彈性叢集實例。

Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本會使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。

Azure Resource Manager 是 Azure 的部署和管理服務。 其提供管理層,可讓您建立、更新和刪除您 Azure 帳戶中的資源。 您可以使用存取控制、鎖定和標記等管理功能,在部署後保護及組織您的資源。 若要了解 Azure Resource Manager 範本,請參閱範本部署概觀

先決條件

具有有效訂用帳戶的 Azure 帳戶。 建立免費帳戶

檢閱範本

適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體是區域內分散式資料庫的父資源。 其可提供適用於叢集的管理原則範圍:防火牆、使用者、角色和組態。

建立一個 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. 輸入資源群組名稱以確認刪除。