快速入门:使用 ARM 模板创建 Azure Database for PostgreSQL 灵活服务器实例

适用于:Azure Database for PostgreSQL 灵活服务器

Azure Database for PostgreSQL 灵活服务器是一项托管服务,用于在云中运行、管理和缩放高度可用的 PostgreSQL 数据库。 可使用 Azure 资源管理器模板(ARM 模板)来预配 Azure Database for PostgreSQL 灵活服务器实例,以便部署多个服务器或在一个服务器上部署多个数据库。

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

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

先决条件

具有活动订阅的 Azure 帐户。 创建一个试用帐户

查看模板

Azure Database for PostgreSQL 灵活服务器实例是某区域中一个或多个数据库的父资源。 它提供应用于其数据库的管理策略范围:登录名、防火墙、用户、角色和配置。

创建一个 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": "16"
    },
    "virtualNetworkExternalId": {
      "type": "string",
      "defaultValue": ""
    },
    "subnetName": {
      "type": "string",
      "defaultValue": ""
    },
    "privateDnsZoneArmResourceId": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
    {
      "type": "Microsoft.DBforPostgreSQL/flexibleServers",
      "apiVersion": "2022-12-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')]"
      }
    }
  ]
}

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

部署模板

$serverName = Read-Host -Prompt "Enter a name for the new Azure Database for PostgreSQL flexible server instance"
$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, chinanorth3) for the resource group"
$adminUser = Read-Host -Prompt "Enter the Azure Database for PostgreSQL flexible server instance'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 中创建。

适用于:Azure Database for PostgreSQL 灵活服务器

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

清理资源

如果希望转到后续步骤,请保留此资源组、服务器和单一数据库。 后续步骤展示了如何使用各种方法连接和查询数据库。

若要删除资源组,请执行以下操作:

适用于:Azure Database for PostgreSQL 灵活服务器

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

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

后续步骤