快速入門:使用 ARM 範本建立適用於 PostgreSQL 的 Azure 資料庫彈性伺服器

適用範圍:適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器

彈性伺服器是一種受控服務,您用來在雲端執行、管理及調整高可用性的 PostgreSQL 資料庫。 您可以使用 Azure Resource Manager 範本 (ARM 範本) 佈建 PostgreSQL Flexible Server,以便在伺服器上部署多個伺服器或多個資料庫。

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

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

Prerequisites

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

檢閱範本

適用於 PostgreSQL 的 Azure 資料庫 Server 是區域內一或多個資料庫的父資源。 提供適用於其資料庫的管理原則範圍︰登入、防火牆、使用者、角色和組態。

建立 postgres-flexible-server-template.json 檔案,並將下列 JSON 指令碼複製到其中。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "administratorLogin": {
      "type": "string"
    },
    "administratorLoginPassword": {
      "type": "secureString"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "serverName": {
      "type": "string"
    },
    "serverEdition": {
      "type": "string",
      "defaultValue": "GeneralPurpose"
    },
    "skuSizeGB": {
      "type": "int",
      "defaultValue": 128
    },
    "dbInstanceType": {
      "type": "string",
      "defaultValue": "Standard_D4ds_v4"
    },
    "haMode": {
      "type": "string",
      "defaultValue": "ZoneRedundant"
    },
    "availabilityZone": {
      "type": "string",
      "defaultValue": "1"
    },
    "version": {
      "type": "string",
      "defaultValue": "12"
    },
    "virtualNetworkExternalId": {
      "type": "string",
      "defaultValue": ""
    },
    "subnetName": {
      "type": "string",
      "defaultValue": ""
    },
    "privateDnsZoneArmResourceId": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
    {
      "type": "Microsoft.DBforPostgreSQL/flexibleServers",
      "apiVersion": "2021-06-01",
      "name": "[parameters('serverName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('dbInstanceType')]",
        "tier": "[parameters('serverEdition')]"
      },
      "properties": {
        "version": "[parameters('version')]",
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "network": {
          "delegatedSubnetResourceId": "[if(empty(parameters('virtualNetworkExternalId')), json('null'), json(format('{0}/subnets/{1}', parameters('virtualNetworkExternalId'), parameters('subnetName'))))]",
          "privateDnsZoneArmResourceId": "[if(empty(parameters('virtualNetworkExternalId')), json('null'), parameters('privateDnsZoneArmResourceId'))]"
        },
        "highAvailability": {
          "mode": "[parameters('haMode')]"
        },
        "storage": {
          "storageSizeGB": "[parameters('skuSizeGB')]"
        },
        "backup": {
          "backupRetentionDays": 7,
          "geoRedundantBackup": "Disabled"
        },
        "availabilityZone": "[parameters('availabilityZone')]"
      }
    }
  ]
}

範本中定義了下列資源:

部署範本

從下列 PowerShell 程式碼區塊中選取 [試試看],以開啟 Azure Cloud Shell。

$serverName = Read-Host -Prompt "Enter a name for the new Azure Database for PostgreSQL server"
$resourceGroupName = Read-Host -Prompt "Enter a name for the new resource group where the server will exist"
$location = Read-Host -Prompt "Enter an Azure region (for example, centralus) for the resource group"
$adminUser = Read-Host -Prompt "Enter the Azure Database for PostgreSQL server's administrator account name"
$adminPassword = Read-Host -Prompt "Enter the administrator password" -AsSecureString

New-AzResourceGroup -Name $resourceGroupName -Location $location # Use this command when you need to create a new resource group for your deployment
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
    -TemplateFile "postgres-flexible-server-template.json" `
    -serverName $serverName `
    -administratorLogin $adminUser `
    -administratorLoginPassword $adminPassword

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

檢閱已部署的資源

請遵循下列步驟,確認是否已在 Azure 中建立您的伺服器。

適用範圍:適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器

  1. Azure 入口網站中,搜尋並選取 [適用於 PostgreSQL 的 Azure 資料庫彈性伺服器]。
  2. 在資料庫清單中,選取您的新伺服器以檢視概觀頁面來管理伺服器。

清除資源

如果您想要繼續進行後續步驟,請保留此資源群組、伺服器和單一資料庫。 後續步驟會示範如何使用不同的方法連接及查詢您的資料庫。

若要刪除資源群組:

適用範圍:適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器

入口網站中選取要刪除的資源群組。

  1. 選取 [刪除資源群組]。
  2. 輸入資源群組名稱以確認刪除

後續步驟