Azure Database for PostgreSQL 搭配彈性叢集是一項管理服務,你可以用來在雲端運行、管理及擴展高可用性的 PostgreSQL 資料庫,並具備橫向擴展功能。 你可以使用 Bicep 範本來為 PostgreSQL 靈活伺服器彈性叢集實例配置 Azure 資料庫。
Bicep 是一種網域特定語言 (DSL),使用宣告式語法來部署 Azure 資源。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。 Bicep 為 Azure 中的基礎結構即程式代碼解決方案提供最佳撰寫體驗。
先決條件
具有有效訂閱的 Azure 帳戶。 免費創建一個。
檢視二頭肌範本
Azure Database for PostgreSQL 彈性伺服器彈性叢集實例是區域內分散式資料庫的母資源。 其可提供適用於叢集的管理原則範圍:防火牆、使用者、角色和組態。
建立一個 elastic-cluster-template.bicep 檔案,然後把以下腳本複製到裡面。
param administratorLogin string
@secure()
param administratorLoginPassword string
param clusterName string
param location string = 'canadacentral'
param clusterSize int = 2
param skuName string = 'Standard_D4ds_v5'
param serverEdition string = 'GeneralPurpose'
param storageSizeGB int = 64
param availabilityZone string = '1'
param backupRetentionDays int = 7
resource server 'Microsoft.DBforPostgreSQL/flexibleServers@2025-08-01' = {
location: location
name: clusterName
properties: {
createMode: 'Default'
version: '17'
administratorLogin: administratorLogin
administratorLoginPassword: administratorLoginPassword
availabilityZone: availabilityZone
Storage: {
StorageSizeGB: storageSizeGB
Autogrow: 'Disabled'
}
Network: {
publicNetworkAccess: 'Enabled'
}
Backup: {
backupRetentionDays: backupRetentionDays
geoRedundantBackup: 'Disabled'
}
highAvailability: {
mode: 'Disabled'
}
cluster: {
clusterSize: clusterSize
}
}
sku: {
name: skuName
tier: serverEdition
}
}
param firewallRules object = {
rules: [
{
name: 'AllowAll'
startIPAddress: '0.0.0.0'
endIPAddress: '255.255.255.255'
}
]
}
// Create one child firewall rule per entry in firewallRules.rules
resource serverFirewallRules 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2025-08-01' = [
for rule in firewallRules.rules: {
name: rule.name
parent: server
properties: {
startIpAddress: rule.startIPAddress
endIpAddress: rule.endIPAddress
}
}
]
Bicep 檔案中定義了這些資源:
部署 Bicep 檔案
使用 Azure CLI 或 Azure PowerShell 來部署 Bicep 檔案。
az login
$resourceGroupName = Read-Host -Prompt "Enter a name for the resource group where the server will exist"
az deployment group create `
--resource-group $resourceGroupName `
--template-file ./elastic-cluster-template.bicep
系統會提示您輸入下列值:
-
clusterName:輸入一個唯一名稱,以識別你的 Azure 資料庫,用於 PostgreSQL 彈性伺服器 elastic cluster 實例。 網域名稱
postgres.database.azure.com會附加在你提供的叢集名稱後面。 叢集名稱只能包含小寫字母、數字及連字號(-)字元。 它必須包含至少 3 到 63 個字元。 -
administratorLogin:輸入你自己的認證帳號,連接伺服器時使用。 例如:
clusterAdmin。 管理員的認證名稱不能是azure_superuser、azure_pg_admin、adminadministratorrootguestpublic或 。 它不能以pg_開頭。 - administratorLoginPassword:輸入伺服器管理員帳戶的新密碼。 其必須包含 8 到 128 個字元。 您的密碼必須包含下列三個類別的字元:英文大寫字母、英文小寫字母、數字 (0 到 9) 和非英數字元 (!、$、#、% 等)。
檢閱已部署的資源
請依照以下步驟確認您的 Azure Database for PostgreSQL 彈性伺服器彈性叢集是否已建立。
- 在 Azure 入口網站中,搜尋並選取 Azure Database for PostgreSQL 彈性伺服器。
- 在資料庫清單中,選擇你的新伺服器以查看 「概覽 」頁面以管理你的彈性叢集。
備註
如果您想要繼續使用此資源群組和彈性叢集,請繼續執行 [相關內容 ] 區段中所列的後續建議步驟。 後續步驟說明如何搭配不同的應用程式分區化模型和設計使用彈性叢集。
清理資源
當你完成彈性叢集環境後,刪除你的彈性叢集資源。
要刪除彈性叢集,請依照以下步驟操作: