快速入门:使用 Azure CLI 创建 Azure Database for PostgreSQL 服务器Quickstart: Create an Azure Database for PostgreSQL server by using the Azure CLI

本快速入门介绍如何使用 Azure CLI 命令在 5 分钟内创建单个 Azure Database for PostgreSQL 服务器。This quickstart shows how to use Azure CLI commands to create a single Azure Database for PostgreSQL server in five minutes. 如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

提示

请考虑使用更简单的 az postgres up Azure CLI 命令(当前为预览版)。Consider using the simpler az postgres up Azure CLI command that's currently in preview. 试用快速入门Try out the quickstart.

先决条件Prerequisites

本文要求在本地运行 Azure CLI 2.0 或更高版本。This article requires you to run 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 the Azure CLI.

需要使用 az login 命令登录到你的帐户。You need to log in to your account by using the az login command. 请注意 id 属性,该属性指的是 Azure 帐户的订阅 ID。Note the id property, which refers to Subscription ID for your Azure account.

az login

使用 az account set 命令选择帐户下的特定订阅 ID。Select the specific subscription ID under your account by using the az account set command. 记下 az login 输出中的 id 值,以用作命令中 subscription 参数的值 。Make a note of the id value from the az login output to use as the value for the subscription argument in the command.

az account set --subscription <subscription id>

如果有多个订阅,请选择应计费的资源所在的相应订阅。If you have multiple subscriptions, choose the appropriate subscription in which the resource should be billed. 若要获取所有订阅,请使用 az account listTo get all your subscriptions, use az account list.

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

使用 az group create 命令创建 Azure 资源组,然后在此资源组中创建 PostgreSQL 服务器。Create an Azure resource group by using the az group create command, and then create your PostgreSQL server inside this resource group. 应提供唯一名称。You should provide a unique name. 以下示例在 chinaeast2 位置创建名为 myresourcegroup 的资源组。The following example creates a resource group named myresourcegroup in the chinaeast2 location.

az group create --name myresourcegroup --location chinaeast2

使用 az postgres server create 命令创建 Azure Database for PostgreSQL 服务器Create an Azure Database for PostgreSQL server by using the az postgres server create command. 一个服务器可以包含多个数据库。A server can contain multiple databases.

az postgres server create --resource-group myresourcegroup --name mydemoserver  --location chinaeast2 --admin-user myadmin --admin-password <server_admin_password> --sku-name GP_Gen5_2 

下面是上述参数的详细信息:Here are the details for the preceding arguments:

设置Setting 示例值Sample value 说明Description
namename mydemoservermydemoserver 标识 Azure Database for PostgreSQL 服务器的唯一名称。Unique name that identifies your Azure Database for PostgreSQL server. 服务器名称只能包含小写字母、数字和连字符 (-) 字符。The server name can contain only lowercase letters, numbers, and the hyphen (-) character. 必须包含 3 到 63 个字符。It must contain 3 to 63 characters. 有关详细信息,请参阅 Azure Database for PostgreSQL 命名规则For more information, see Azure Database for PostgreSQL Naming Rules.
resource-groupresource-group myresourcegroupmyresourcegroup Azure 资源组的名称。Name of the Azure resource group.
locationlocation chinaeast2chinaeast2 服务器的 Azure 位置。Azure location for the server.
admin-useradmin-user myadminmyadmin 用于管理员登录的用户名。Username for the administrator login. 它不能为“azure_superuser”、“admin”、“administrator”、“root”、“guest”或“public” 。It can't be azure_superuser , admin , administrator , root , guest , or public.
admin-passwordadmin-password 安全密码secure password 管理员用户的密码。Password of the administrator user. 密码必须包含以下三个类别的 8 到 128 个字符:英文大写字母、英文小写字母、数字和非字母数字字符。It must contain 8 to 128 characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
sku-namesku-name GP_Gen5_2GP_Gen5_2 定价层和计算配置的名称。Name of the pricing tier and compute configuration. 请遵循简写约定 {pricing tier} {compute generation} {vCores}。Follow the convention {pricing tier} {compute generation} {vCores} in shorthand. 有关详细信息,请参阅 Azure Database for PostgreSQL 定价For more information, see Azure Database for PostgreSQL pricing.

重要

配置服务器级防火墙规则Configure a server-level firewall rule

默认情况下,你创建的服务器不可公开访问,并受防火墙规则保护。By default, the server that you created is not publicly accessible and is protected with firewall rules. 可以使用 az postgres server firewall-rule create 命令在服务器上配置防火墙规则,以便为本地环境提供访问权限以连接到服务器。You can configure the firewall rules on your server by using the az postgres server firewall-rule create command to give your local environment access to connect to the server.

以下示例创建名为 AllowMyIP 的防火墙规则,该规则允许从特定的 IP 地址 (192.168.0.1) 进行连接。The following example creates a firewall rule called AllowMyIP that allows connections from a specific IP address, 192.168.0.1. 替代与要从其进行连接的地址相对应的 IP 地址或 IP 地址范围。Replace the IP address or range of IP addresses that corresponds to where you'll be connecting from. 如果你不知道 IP 地址,请转到 WhatIsMyIPAddress.com 获取它。If you don't know your IP address, go to WhatIsMyIPAddress.com to get it.

az postgres server firewall-rule create --resource-group myresourcegroup --server mydemoserver --name AllowMyIP --start-ip-address 192.168.0.1 --end-ip-address 192.168.0.1

备注

若要避免连接问题,请确保网络防火墙允许端口 5432。To avoid connectivity issues, make sure your network's firewall allows port 5432. Azure Database for PostgreSQL 服务器使用该端口。Azure Database for PostgreSQL servers use that port.

获取连接信息Get the connection information

若要连接到服务器,请提供主机信息和访问凭据。To connect to your server, provide host information and access credentials.

az postgres server show --resource-group myresourcegroup --name mydemoserver

结果采用 JSON 格式。The result is in JSON format. 记下 administratorLogin 和 fullyQualifiedDomainName 值 。Make a note of the administratorLogin and fullyQualifiedDomainName values.

{
  "administratorLogin": "myadmin",
  "earliestRestoreDate": null,
  "fullyQualifiedDomainName": "mydemoserver.postgres.database.chinacloudapi.cn",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforPostgreSQL/servers/mydemoserver",
  "location": "chinaeast2",
  "name": "mydemoserver",
  "resourceGroup": "myresourcegroup",
  "sku": {
    "capacity": 2,
    "family": "Gen5",
    "name": "GP_Gen5_2",
    "size": null,
    "tier": "GeneralPurpose"
  },
  "sslEnforcement": "Enabled",
  "storageProfile": {
    "backupRetentionDays": 7,
    "geoRedundantBackup": "Disabled",
    "storageMb": 5120
  },
  "tags": null,
  "type": "Microsoft.DBforPostgreSQL/servers",
  "userVisibleState": "Ready",
  "version": "9.6"
}

使用 psql 连接到 Azure Database for PostgreSQL 服务器Connect to the Azure Database for PostgreSQL server by using psql

psql 客户端是连接到 PostgreSQL 服务器的热门选择。The psql client is a popular choice for connecting to PostgreSQL servers. 还可以在本地环境中使用 psql(如果可用)连接到服务器。You can connect to your server by using psql on your local environment if you have it available. 将使用新的 PostgreSQL 服务器自动创建空数据库 postgres。An empty database, postgres , is automatically created with a new PostgreSQL server. 可以使用该数据库与 psql 连接,如下面的代码所示。You can use that database to connect with psql, as shown in the following code.

psql --host=mydemoserver.postgres.database.chinacloudapi.cn --port=5432 --username=myadmin@mydemoserver --dbname=postgres

提示

如果更喜欢使用 URL 路径连接到 Postgres,则 URL 会使用 %40 对用户名中的 @ 符号进行编码。If you prefer to use a URL path to connect to Postgres, URL encode the @ sign in the username with %40. 例如,psql 的连接字符串将是:For example, the connection string for psql would be:

psql postgresql://myadmin%40mydemoserver@mydemoserver.postgres.database.chinacloudapi.cn:5432/postgres

清理资源Clean up resources

如果不需要将这些资源用于其他快速入门或教程,则可通过运行以下命令将其删除。If you don't need these resources for another quickstart or tutorial, you can delete them by running the following command.

az group delete --name myresourcegroup

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

az postgres server delete --resource-group myresourcegroup --name mydemoserver

后续步骤Next steps