你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure Database for PostgreSQL 是一项托管服务,可用于在云中运行、管理和缩放高度可用的 PostgreSQL 数据库。
本快速入门介绍如何使用 Azure 门户、Azure CLI 或 Azure 资源管理器 (ARM) 模板创建 Azure Database for PostgreSQL 灵活服务器实例。
先决条件
在开始之前,请确保具备:
- 一份 Azure 订阅。 如果没有帐户,请创建 一个免费的 Azure 帐户。
- 在订阅中创建资源的相应权限。
了解要创建的内容
Azure Database for PostgreSQL 灵活服务器实例包括:
- 一组配置的 计算和存储资源。
- 在 Azure 资源组中进行部署。
- 默认创建的
postgres数据库。 -
azure_maintenance托管服务进程的数据库。 - 用于
azure_sys查询存储和索引优化功能的数据库。
注释
如果通过内置的 PgBouncer 连接池器进行连接,则连接通常使用端口 5432 或端口 6432。
使用 Azure 门户创建服务器
导航到创建向导
- 登录到 Azure 门户。
- 选择左上角的 “创建资源 ”。
- 在“类别”下,选择“数据库”。
- 查找并选择 Azure Database for PostgreSQL 灵活服务器。
- 选择 创建。
配置基本设置
项目详细信息
| 设置 | 建议值 | 注释 |
|---|---|---|
| Subscription | 你的订阅 | 选择资源计费位置 |
| 资源组 | myresourcegroup | 新建或选择现有 |
服务器详细信息
| 设置 | 建议值 | Description |
|---|---|---|
| 服务器名称 | mydemoserver-pgsql | 必须全局唯一。 域.postgres.database.azure.com被自动添加 |
| Region | 离你最近的区域 | 考虑合规性、数据驻留、定价和用户的接近度 |
| PostgreSQL 版本 | 最新可用 | 当前支持: 18、17、16、15、14、13、12、11 |
| 工作负荷类型 | 开发 | 开发环境使用可突发性能的 SKU(库存单位)。 生产中使用通用型或内存优化型。 |
| 可用性区域 | 无首选项 | 与应用程序并置非常有用 |
高可用性
| 选项 | SLA | Description |
|---|---|---|
| Disabled | 99.9% | 没有备用服务器的单个服务器 |
| 同一区域 | 99.95% | 在同一可用区内待命 |
| 区域冗余 | 99.99% | 其他可用性区域中的待机 |
Authentication
| 设置 | Description | Recommended |
|---|---|---|
| 身份验证方法 | 用户如何进行身份验证 |
-
仅 PostgreSQL 身份验证 (快速入门) - Microsoft Entra 身份验证 (用于生产) - 两者 (为灵活性) |
| 管理员用户名 | adminuser | - 必须为 1-63 个字符 - 仅数字和字母 - 不能从 pg_ 开始- 不能是系统保留名称 |
| 密码 | 复杂密码 | 包含大写、小写、数字和特殊字符的 8-128 个字符 |
配置计算和存储
选择“ 配置服务器 ”以自定义:
计算层
| 层 | 用例 | Description |
|---|---|---|
| 可突发 | 开发 | 对于不需要持续使用完整 CPU 资源的工作负载 |
| 常规用途 | 生产 | 最常见的生产工作负荷 |
| 内存优化 | 高内存工作负荷 | 需要高内存与 CPU 比率的工作负荷 |
存储设置
| 设置 | 以后可以更改 | Description |
|---|---|---|
| 存储类型 | ❌ 否 | 高级 SSD 或高级 SSD v2 |
| 存储大小 | ✅ 是(仅增加) | 创建后无法收缩 |
| 性能层 | ✅ 是 | 控制 IOPS 和吞吐量 |
| 存储自动增长 | ✅ 是 | 接近限制时自动展开 |
备份设置
| 设置 | 以后可以更改 | Description |
|---|---|---|
| 备份保留期 | ✅ 是 | 7-35 天 |
| 备份冗余 | ❌ 否 | 本地冗余、区域冗余或异地冗余 |
| 异地冗余 | ❌ 否 | 仅在 Azure 配对区域中可用 |
配置网络
选择连接方法(创建后无法更改):
公共访问(允许的 IP 地址)
使用防火墙规则通过公共终结点进行连接。
设置:
| 设置 | Description |
|---|---|
| 允许公共访问 | 启用公共访问以配置防火墙规则 |
| 允许 Azure 服务 | 允许来自所有 Azure 服务的连接 |
| 添加当前客户端 IP | 将您的 IP 地址添加到允许列表中 |
专用访问(虚拟网络集成)
通过虚拟网络中的专用终结点进行连接。 有关详细信息,请参阅 Azure Database for PostgreSQL 的专用访问网络。
配置安全性
| 设置 | 以后可以更改 | 选项 |
|---|---|---|
| 数据加密密钥 | ❌ 否 | 服务管理或客户管理 |
添加资源标记(可选)
通过名称-值对来组织资源:
| Name | 价值 | 目的 |
|---|---|---|
| 环境 | 开发 | 标识环境类型 |
| CostCenter | IT-Dept | 按部门跟踪成本 |
| 所有者 | admin@contoso.com | 确定责任方 |
查看并创建
- 选择“查看 + 创建”。
- 查看所有配置。
- 选择“ 创建 ”以部署。
部署通常需要 5-10 分钟。 完成后,选择“ 转到资源” 以访问服务器。
使用 Azure CLI 创建服务器
CLI 的先决条件
对于此脚本,请在本地使用 Azure CLI,因为在 Cloud Shell 中运行所花费的时间较长。
登录到 Azure
使用以下脚本通过特定订阅登录。
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
如果使用 Azure Cloud Shell,则已登录。
使用 CLI 创建服务器
使用一个命令创建服务器:
az postgres flexible-server create \
--resource-group myresourcegroup \
--name mydemoserver-pgsql \
--location eastus \
--admin-user myadmin \
--admin-password <password> \
--sku-name Standard_D4ds_v5 \
--tier GeneralPurpose \
--public-access 0.0.0.0 \
--storage-size 128 \
--tags "Environment=Development"
CLI 参数参考
| 参数 | Description | Example |
|---|---|---|
--resource-group |
资源组名称 | myresourcegroup |
--name |
全局唯一的服务器名称 | mydemoserver-pgsql |
--location |
Azure 区域 | eastus |
--admin-user |
管理员用户名 | myadmin |
--admin-password |
管理员密码 | YourPassword123! |
--sku-name |
计算 SKU | Standard_D4ds_v5 |
--tier |
计算层 | 可突发、通用型、内存优化型 |
--storage-size |
以 GB 为单位的存储 | 128 |
--public-access |
允许的 IP 地址 | 0.0.0.0 (所有 Azure 服务)、IP 地址或 IP 范围 |
--version |
PostgreSQL 版本 | 16 |
--high-availability |
HA 模式 | Disabled、SameZone、ZoneRedundant |
--backup-retention |
备份保留天数 | 7-35 |
高级 CLI 示例
创建区域冗余高可用性服务器:
az postgres flexible-server create \
--resource-group myresourcegroup \
--name mydemoserver-pgsql-ha \
--location eastus \
--admin-user myadmin \
--admin-password <password> \
--sku-name Standard_D4ds_v5 \
--tier GeneralPurpose \
--storage-size 256 \
--storage-type PremiumV2_LRS \
--high-availability ZoneRedundant \
--zone 1 \
--standby-zone 2 \
--backup-retention 14 \
--public-access 0.0.0.0
使用 ARM 模板创建服务器
ARM 模板概述
使用 Azure 资源管理器 (ARM) 模板可将基础结构定义为代码。 使用模板进行可重复部署。
最小 ARM 模板
将此文件另存为 postgres-server-template.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"serverName": {
"type": "string",
"metadata": {
"description": "Server name must be globally unique"
}
},
"administratorLogin": {
"type": "string",
"minLength": 1,
"maxLength": 63,
"metadata": {
"description": "Administrator username"
}
},
"administratorLoginPassword": {
"type": "securestring",
"minLength": 8,
"metadata": {
"description": "Administrator password"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Server location"
}
}
},
"resources": [
{
"type": "Microsoft.DBforPostgreSQL/flexibleServers",
"apiVersion": "2024-08-01",
"name": "[parameters('serverName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_D4ds_v5",
"tier": "GeneralPurpose"
},
"properties": {
"administratorLogin": "[parameters('administratorLogin')]",
"administratorLoginPassword": "[parameters('administratorLoginPassword')]",
"version": "16",
"storage": {
"storageSizeGB": 128,
"type": "Premium_LRS",
"autoGrow": "Enabled"
},
"backup": {
"backupRetentionDays": 7,
"geoRedundantBackup": "Disabled"
},
"network": {
"publicNetworkAccess": "Enabled"
},
"highAvailability": {
"mode": "Disabled"
}
}
}
],
"outputs": {
"serverFQDN": {
"type": "string",
"value": "[reference(parameters('serverName')).fullyQualifiedDomainName]"
}
}
}
部署 ARM 模板
az group create --name myresourcegroup --location eastus
az deployment group create \
--resource-group myresourcegroup \
--template-file postgres-server-template.json \
--parameters \
serverName=mydemoserver-pgsql \
administratorLogin=myadmin \
administratorLoginPassword=<password>
获取连接信息
创建服务器后,检索连接详细信息:
使用 Azure 门户
- 请进入 Azure 门户中的服务器。
- 打开“概览”页面。
- 复制以下值:
-
服务器名称 (终结点):
mydemoserver-pgsql.postgres.database.azure.com -
管理员登录:
myadmin
-
服务器名称 (终结点):
使用 Azure CLI
az postgres flexible-server show \
--resource-group myresourcegroup \
--name mydemoserver-pgsql \
--query "{serverName:fullyQualifiedDomainName, adminUser:administratorLogin}" \
--output table
使用 psql 进行连接
安装 psql
如果没有 PostgreSQL 客户端工具, 请下载适用于平台的 PostgreSQL 。
连接到您的服务器
psql "host=mydemoserver-pgsql.postgres.database.azure.com port=5432 dbname=postgres user=myadmin sslmode=require"
出现提示时,输入在创建服务器期间设置的管理员密码。
连接字符串格式
host=<server-name>.postgres.database.azure.com port=5432 dbname=<database-name> user=<admin-user> password=<password> sslmode=require
验证连接
连接后,应会看到:
psql (14.13, server 16.4)
WARNING: psql major version 14, server major version 16.
Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=>
创建数据库
CREATE DATABASE user_database;
\c user_database
\q
排查连接问题
防火墙阻止连接
如果看到:
connection to server at "mydemoserver-pgsql.postgres.database.azure.com" (###.###.###.###), port 5432 failed: Connection timed out
解决方案:将 IP 地址添加到防火墙规则:
az postgres flexible-server firewall-rule create \
--resource-group myresourcegroup \
--name mydemoserver-pgsql \
--rule-name AllowMyIP \
--start-ip-address <your-ip> \
--end-ip-address <your-ip>
SSL 必需,但未配置
确保连接字符串包括 sslmode=require。
清理资源
完成快速入门后,请删除资源以避免费用。
删除整个资源组
az group delete --name myresourcegroup --yes
仅删除服务器
az postgres flexible-server delete \
--resource-group myresourcegroup \
--name mydemoserver-pgsql \
--yes