在 適用於 PostgreSQL 的 Azure 資料庫 flexible server 使用 Azure CLI 建立伺服器並配置虛擬網路規則

這個範例 CLI 腳本會建立一個 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器,並設定虛擬網路規則。

如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。

先決條件

範例腳本

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以前往 https://shell.azure.com ,在新的瀏覽器分頁中開啟 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

Cloud Shell 會在首次登入的帳戶下自動完成身份驗證。 使用下列腳本使用不同的訂用帳戶登入,並將 subscriptionId 取代為您的 Azure 訂用帳戶標識碼。

如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。

subscription="subscriptionId" # Set Azure subscription ID here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱或以互動方式登入。

執行指令碼

# Create a PostgreSQL server and configure a vNet rule

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="create-postgresql-server"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
vNet="vNet-$randomIdentifier"
vNetAddressPrefix="10.0.0.0/16"
subnet="subnet-$randomIdentifier"
subnetAddressPrefix="10.0.1.0/24"
rule="rule-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"

echo "Using resource group $resourceGroup with login: $login, password: $password..."

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku

# Get available service endpoints for Azure region output is JSON
echo "List of available service endpoints for $location"
az network vnet list-endpoint-services --location "$location"

# Add Azure SQL service endpoint to a subnet while creating the virtual network
echo "Adding service endpoint to $subnet in $vNet"
az network vnet create --resource-group $resourceGroup --name $vNet --address-prefixes $vNetAddressPrefix --location "$location"

# Creates the service endpoint
echo "Creating a service endpoint to $subnet in $vNet"
az network vnet subnet create --resource-group $resourceGroup --name $subnet --vnet-name $vNet --address-prefix $subnetAddressPrefix --service-endpoints Microsoft.SQL

# View service endpoints configured on a subnet
echo "Viewing the service endpoint to $subnet in $vNet"
az network vnet subnet show --resource-group $resourceGroup --name $subnet --vnet-name $vNet

# Create a VNet rule on the server to secure it to the subnet
# Note: resource group (-g) parameter is where the database exists.
# VNet resource group if different should be specified using subnet id (URI) instead of subnet, VNet pair.
echo "Creating a VNet rule on $server to secure it to $subnet in $vNet"
az postgres server vnet-rule create --name $rule --resource-group $resourceGroup --server $server --vnet-name $vNet --subnet $subnet

清理資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $resourceGroup

範例參考

此指令碼會使用下表中簡述的命令:

命令 注意事項
請輸入命令:az group create 來建立群組。 建立資源群組來儲存所有資源。
az postgresql server create (建立 PostgreSQL 伺服器) 建立一個用來裝載資料庫的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器。
az network virtual network list-endpoint-services 列出哪些服務支援所指定區域中的虛擬網路服務通道。
az network 虛擬網路建立 建立虛擬網路。
AZ Network 虛擬網路子網創建 建立子網路,並建立現有 NSG 與路由表的關聯。
az network virtual network subnet show 顯示子網路的詳細資料。
az postgresql server vnet-rule create 建立虛擬網路規則,允許存取 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器。
az 群組刪除 刪除包含所有巢狀資源的資源群組。