您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 Azure CLI 命令、az postgres up(预览版)创建 Azure Database for PostgreSQL - 单一服务器Quickstart: Use an Azure CLI command, az postgres up (preview), to create an Azure Database for PostgreSQL - Single Server

重要

az postgres up Azure CLI 命令现为预览版。The az postgres up Azure CLI command is in preview.

用于 PostgreSQL 的 Azure 数据库是一种托管服务,可用于在云中运行、管理和缩放具有高可用性的 PostgreSQL 数据库。Azure Database for PostgreSQL is a managed service that enables you to run, manage, and scale highly available PostgreSQL databases in the cloud. Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 此快速入门介绍了如何使用 az postgres up 命令和 Azure CLI 来创建 Azure Database for PostgreSQL 服务器。This quickstart shows you how to use the az postgres up command to create an Azure Database for PostgreSQL server using the Azure CLI. 除了创建服务器,az postgres up 命令还会创建示例数据库、在数据库中创建根用户、为 Azure 服务打开防火墙,并为客户端计算机创建默认防火墙规则。In addition to creating the server, the az postgres up command creates a sample database, a root user in the database, opens the firewall for Azure services, and creates default firewall rules for the client computer. 这些默认值有助于加快开发过程。These defaults help to expedite the development process.

先决条件Prerequisites

如果没有 Azure 订阅,请在开始之前创建一个免费帐户。If you don't have an Azure subscription, create a free account before you begin.

本文要求在本地运行 Azure CLI 2.0 或更高版本。This article requires that you're running the Azure CLI version 2.0 or later locally. 若要查看安装的版本,请运行 az --version 命令。To see the version installed, run the az --version command. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

需要使用 az login 命令登录账户。You'll need to sign in to your account using the az login command. 记下命令行输出中相应订阅名称的 ID 属性。Note the ID property from the command output for the corresponding subscription name.

az login

如果有多个订阅,请选择应计费的资源所在的相应订阅。If you have multiple subscriptions, choose the appropriate subscription in which the resource should be billed. 使用 az account set 命令选择帐户下的特定订阅 ID。Select the specific subscription ID under your account using az account set command. 将 az login 输出中的你的订阅的订阅 ID 属性替换到订阅 ID 占位符中。Substitute the subscription ID property from the az login output for your subscription into the subscription ID placeholder.

az account set --subscription <subscription id>

创建 Azure Database for PostgreSQL 服务器Create an Azure Database for PostgreSQL server

要使用命令,请安装 db-up 扩展。To use the commands, install the db-up extension. 如果返回错误,则请确保已安装最新版本的 Azure CLI。If an error is returned, ensure you have installed the latest version of the Azure CLI. 请参阅安装 Azure CLISee Install Azure CLI.

az extension add --name db-up

若要使用以下命令创建 Azure Database for PostgreSQL 服务器:Create an Azure Database for PostgreSQL server using the following command:

az postgres up

服务器是使用以下默认值创建的(除非手动替代它们):The server is created with the following default values (unless you manually override them):

设置Setting 默认值Default value 说明Description
server-nameserver-name 系统生成的System generated 用于标识用于 PostgreSQL 的 Azure 数据库服务器的唯一名称。A unique name that identifies your Azure Database for PostgreSQL server.
resource-groupresource-group 系统生成的System generated 新的 Azure 资源组。A new Azure resource group.
sku-namesku-name GP_Gen5_2GP_Gen5_2 SKU 的名称。The name of the sku. 请遵循简写约定 {pricing tier}_{compute generation}_{vCores}。Follows the convention {pricing tier}_{compute generation}_{vCores} in shorthand. 默认为带 2 个 vCore 的常规用途 Gen5 服务器。The default is a General Purpose Gen5 server with 2 vCores. 有关各层的详细信息,请参阅定价页See our pricing page for more information about the tiers.
backup-retentionbackup-retention 77 备份保留的期限。How long a backup is retained. 单位为天。Unit is days.
geo-redundant-backupgeo-redundant-backup 已禁用Disabled 是否应该为此服务启用异地冗余备份。Whether geo-redundant backups should be enabled for this server or not.
locationlocation westus2westus2 服务器的 Azure 位置。The Azure location for the server.
ssl-enforcementssl-enforcement 已禁用Disabled 是否应该为此服务启用 ssl。Whether ssl should be enabled or not for this server.
storage-sizestorage-size 51205120 服务器的存储容量(单位是兆字节)。The storage capacity of the server (unit is megabytes).
版本version 1010 PostgreSQL 主版本。The PostgreSQL major version.
admin-useradmin-user 系统生成的System generated 管理员用户名。The username for the administrator.
admin-passwordadmin-password 系统生成的System generated 管理员用户的密码。The password of the administrator user.

备注

有关 az postgres up 命令及其附加参数的详细信息,请参阅 Azure CLI 文档For more information about the az postgres up command and its additional parameters, see the Azure CLI documentation.

服务器创建后,随附有以下设置:Once your server is created, it comes with the following settings:

  • 创建了一条名为“devbox”的防火墙规则。A firewall rule called "devbox" is created. Azure CLI 尝试检测运行 az postgres up 命令的计算机的 IP 地址以及该 IP 面向的允许列表。The Azure CLI attempts to detect the IP address of the machine the az postgres up command is run from and whitelists that IP address.
  • “允许访问 Azure 服务”设置为“开”。"Allow access to Azure services" is set to ON. 此设置会将服务器的防火墙配置为接受来自所有 Azure 资源(包括非订阅中的资源)的连接。This setting configures the server's firewall to accept connections from all Azure resources, including resources not in your subscription.
  • 创建了一个名为“sampledb”的空数据库An empty database named "sampledb" is created
  • 创建了一个特权为“sampledb”且名为“root”的新用户A new user named "root" with privileges to "sampledb" is created

备注

Azure Database for PostgreSQL 通过端口 5432 进行通信。Azure Database for PostgreSQL communicates over port 5432. 从企业网络内部进行连接时,该网络的防火墙可能不允许经端口 5432 的出站流量。When connecting from within a corporate network, outbound traffic over port 5432 may not be allowed by your network's firewall. 让 IT 部门打开端口 5432 以连接到你的服务器。Have your IT department open port 5432 to connect to your server.

获取连接信息Get the connection information

在完成 az postgres up 命令后,将向你返回一个常用编程语言连接字符串列表。After the az postgres up command is completed, a list of connection strings for popular programming languages is returned to you. 这些连接字符串使用你新创建的 Azure Database for PostgreSQL 服务器的特定属性进行了预配置。These connection strings are pre-configured with the specific attributes of your newly created Azure Database for PostgreSQL server.

可使用 az postgres show-connection-string 命令再次列出这些连接字符串。You can use the az postgres show-connection-string command to list these connection strings again.

清理资源Clean up resources

请使用以下命令清除在此快速入门中创建的所有资源。Clean up all resources you created in the quickstart using the following command. 此命令会删除 Azure Database for PostgreSQL 服务器和资源组。This command deletes the Azure Database for PostgreSQL server and the resource group.

az postgres down --delete-group

若要删除新创建的服务器,可运行 az postgres down 命令。If you would just like to delete the newly created server, you can run az postgres down command.

az postgres down

后续步骤Next steps