快速入门:使用 Azure CLI 创建 Azure Database for PostgreSQL - 单一服务器Quickstart: Create an Azure Database for PostgreSQL - Single Server using the Azure CLI

Tip

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

用于 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. 本快速入门指南介绍了如何使用 Azure CLI 在 Azure 资源组中创建 Azure Database for PostgreSQL 服务器。This quickstart shows you how to create an Azure Database for PostgreSQL server in an Azure resource group using the Azure CLI.

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

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

要本地运行 CLI,需要使用 az login 命令登录你的帐户。If you are running the CLI locally, you 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 ID property from the az login output for your subscription into the subscription ID placeholder.

az account set --subscription <subscription id>

创建资源组Create a resource group

使用 az group create 命令创建 Azure 资源组Create an Azure resource group using the az group create command. 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed as a group. 应提供唯一名称。You should provide a unique name. 以下示例在 chinaeast 位置创建名为 myresourcegroup 的资源组。The following example creates a resource group named myresourcegroup in the chinaeast location.

az group create --name myresourcegroup --location chinaeast

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

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

设置Setting 示例值Sample value 说明Description
namename mydemoservermydemoserver 选择用于标识 Azure Database for PostgreSQL 服务器的唯一名称。Choose a 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 from 3 to 63 characters.
resource-groupresource-group myresourcegroupmyresourcegroup 提供 Azure 资源组的名称。Provide the name of the Azure resource group.
sku-namesku-name GP_Gen4_2GP_Gen4_2 SKU 的名称。The name of the sku. 遵循约定“{定价层}_{计算代系}_{vCores}”的简写形式。Follows the convention {pricing tier}_{compute generation}_{vCores} in shorthand. 参阅下表详细了解 sku-name 参数。See below this table for more information about the sku-name parameter.
backup-retentionbackup-retention 77 备份保留时间。How long a backup should be retained. 单位为天。Unit is days. 范围为 7-35。Range is 7-35.
geo-redundant-backupgeo-redundant-backup 已禁用Disabled 是否应为此服务器启用异地冗余备份。Whether geo-redundant backups should be enabled for this server or not. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
locationlocation chinaeastchinaeast 服务器的 Azure 位置。The Azure location for the server.
ssl-enforcementssl-enforcement EnabledEnabled 是否应为此服务器启用 SSL。Whether ssl should be enabled or not for this server. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
storage-sizestorage-size 5120051200 服务器的存储容量(以 MB 为单位)。The storage capacity of the server (unit is megabytes). 有效的 storage-size 最小为 5120MB,以 1024MB 为增量递增。Valid storage-size is minimum 5120MB and increases in 1024MB increments. 有关存储大小限制的详细信息,请参阅定价层文档。See the pricing tiers document for more information about storage size limits.
版本version 9.69.6 PostgreSQL 主版本。The PostgreSQL major version.
admin-useradmin-user myadminmyadmin 管理员的登录用户名。The username for the administrator login. 不能是 azure_superuseradminadministratorrootguestpublicIt cannot be azure_superuser, admin, administrator, root, guest, or public.
admin-passwordadmin-password 安全密码secure password 管理员用户的密码。The password of the administrator user. 该密码必须包含 8 到 128 个字符。It must contain between 8 and 128 characters. 密码必须包含以下三个类别的字符:英文大写字母、英文小写字母、数字和非字母数字字符。Your password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.

sku-name 参数值遵循 {定价层}_{计算层代}_{vCore 数} 约定,如以下示例中所示:The sku-name parameter value follows the convention {pricing tier}_{compute generation}_{vCores} as in the examples below:

  • --sku-name B_Gen4_1 映射到基本、第 4 代和 1 个 vCore。--sku-name B_Gen4_1 maps to Basic, Gen 4, and 1 vCore. 此选项是可用的最小 SKU。This option is the smallest SKU available.
  • --sku-name GP_Gen5_32 映射到常规用途、第 5 层和 32 个 vCore。--sku-name GP_Gen5_32 maps to General Purpose, Gen 5, and 32 vCores.
  • --sku-name MO_Gen5_2 映射到内存优化、第 5 层和 2 个 vCore。--sku-name MO_Gen5_2 maps to Memory Optimized, Gen 5, and 2 vCores.

请参阅定价层文档来了解适用于每个区域和每个层的有效值。Please see the pricing tiers documentation to understand the valid values per region and per tier.

下面的示例使用服务器管理员登录名 myadmin 在资源组 myresourcegroup 中创建位于“中国东部”区域的名为 mydemoserver 的 PostgreSQL 9.6 服务器。The following example creates a PostgreSQL 9.6 server in China East named mydemoserver in your resource group myresourcegroup with server admin login myadmin. 这是第 4 代常规用途服务器,带有 2 个 vCore。This is a Gen 4 General Purpose server with 2 vCores. 用自己的值替换 <server_admin_password>Substitute the <server_admin_password> with your own value.

az postgres server create --resource-group myresourcegroup --name mydemoserver  --location chinaeast --admin-user myadmin --admin-password <server_admin_password> --sku-name GP_Gen4_2 --version 9.6

Note

如果轻量级计算和 I/O 足以满足工作负荷要求,请考虑使用“基本”定价层。Consider using the Basic pricing tier if light compute and I/O are adequate for your workload. 请注意,在“基本”定价层中创建的服务器以后不能扩展到“常规用途”或“内存优化”定价层。Note that servers created in the Basic pricing tier cannot later be scaled to General Purpose or Memory Optimized. 有关详细信息,请参阅定价页See the pricing page for more information.

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

使用 az postgres server firewall-rule create 命令创建 Azure PostgreSQL 服务器级防火墙规则。Create an Azure PostgreSQL server-level firewall rule with the az postgres server firewall-rule create command. 服务器级防火墙规则允许外部应用程序(如 psqlPgAdmin)通过 Azure PostgreSQL 服务防火墙连接到服务器。A server-level firewall rule allows an external application, such as psql or PgAdmin to connect to your server through the Azure PostgreSQL service firewall.

可以设置涵盖某个 IP 范围的防火墙规则,以便通过网络进行连接。You can set a firewall rule that covers an IP range to be able to connect from your network. 下面的示例使用 az postgres server firewall-rule create 为单个 IP 地址创建防火墙规则 AllowMyIPThe following example uses az postgres server firewall-rule create to create a firewall rule AllowMyIP for a single IP address.

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

Note

Azure PostgreSQL 服务器通过端口 5432 进行通信。Azure PostgreSQL server 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,以便连接到 Azure PostgreSQL 服务器。Have your IT department open port 5432 to connect to your Azure PostgreSQL server.

获取连接信息Get the connection information

若要连接到服务器,需要提供主机信息和访问凭据。To connect to your server, you need to 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.

{
  "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": "chinaeast",
  "name": "mydemoserver",
  "resourceGroup": "myresourcegroup",
  "sku": {
    "capacity": 2,
    "family": "Gen4",
    "name": "GP_Gen4_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 连接到 PostgreSQL 数据库Connect to PostgreSQL database using psql

如果客户端计算机已安装 PostgreSQL,则可以使用 psql 的本地实例连接到 Azure PostgreSQL 服务器。If your client computer has PostgreSQL installed, you can use a local instance of psql to connect to an Azure PostgreSQL server. 现在使用 psql 命令行实用工具连接到 Azure PostgreSQL 服务器。Let's now use the psql command-line utility to connect to the Azure PostgreSQL server.

  1. 运行以下 psql 命令连接到 Azure Database for PostgreSQL 服务器Run the following psql command to connect to an Azure Database for PostgreSQL server

    psql --host=<servername> --port=<port> --username=<user@servername> --dbname=<dbname>
    

    例如,以下命令使用访问凭据连接到 PostgreSQL 服务器 mydemoserver.postgres.database.chinacloudapi.cn 上名为“postgres”的默认数据库。For example, the following command connects to the default database called postgres on your PostgreSQL server mydemoserver.postgres.database.chinacloudapi.cn using access credentials. 提示输入密码时,输入之前选择的 <server_admin_password>Enter the <server_admin_password> you chose when prompted for password.

    psql --host=mydemoserver.postgres.database.chinacloudapi.cn --port=5432 --username=myadmin@mydemoserver --dbname=postgres
    
  2. 连接到服务器后,在出现提示时创建空数据库。Once you are connected to the server, create a blank database at the prompt.

    CREATE DATABASE mypgsqldb;
    
  3. 出现提示时,请执行以下命令,将连接切换到新建的数据库 mypgsqldb:At the prompt, execute the following command to switch connection to the newly created database mypgsqldb:

    \c mypgsqldb
    

使用 pgAdmin 连接到 PostgreSQL 服务器Connect to the PostgreSQL Server using pgAdmin

pgAdmin 是用于 PostgreSQL 的开源工具。pgAdmin is an open-source tool used with PostgreSQL. 可以从 pgAdmin 网站安装 pgAdmin。You can install pgAdmin from the pgAdmin website. 你所使用的 pgAdmin 版本可能不同于本快速入门中使用的版本。The pgAdmin version you're using may be different from what is used in this Quickstart. 如果需要更多指南,请阅读 pgAdmin 文档。Read the pgAdmin documentation if you need additional guidance.

  1. 在客户端计算机上打开 pgAdmin 应用程序。Open the pgAdmin application on your client computer.

  2. 从工具栏转到“对象”,将鼠标指针悬停在“创建”上,然后选择“服务器”。From the toolbar go to Object, hover over Create, and select Server.

  3. 在“创建 - 服务器”对话框中的“常规”选项卡上,为服务器输入唯一的友好名称,例如 mydemoserverIn the Create - Server dialog box, on the General tab, enter a unique friendly name for the server, such as mydemoserver.

    “常规”选项卡

  4. 在“创建 - 服务器”对话框中的“连接”选项卡上,填写设置表。In the Create - Server dialog box, on the Connection tab, fill in the settings table.

    “连接”选项卡

    pgAdmin 参数pgAdmin parameter ValueValue 说明Description
    主机名/地址Host name/address 服务器名称Server name 此前在创建用于 PostgreSQL 的 Azure 数据库服务器时使用过的服务器名称值。The server name value that you used when you created the Azure Database for PostgreSQL server earlier. 示例服务器为 mydemoserver.postgres.database.chinacloudapi.cnOur example server is mydemoserver.postgres.database.chinacloudapi.cn. 请使用完全限定的域名 (*.postgres.database.chinacloudapi.cn),如示例中所示。Use the fully qualified domain name (*.postgres.database.chinacloudapi.cn) as shown in the example. 如果不记得服务器名称,请按上一部分的步骤操作,以便获取连接信息。If you don't remember your server name, follow the steps in the previous section to get the connection information.
    端口Port 54325432 连接到用于 PostgreSQL 的 Azure 数据库服务器时使用的端口。The port to use when you connect to the Azure Database for PostgreSQL server.
    维护数据库Maintenance database postgrespostgres 系统生成的默认数据库名称。The default system-generated database name.
    用户名Username 服务器管理员登录名Server admin login name 此前在创建用于 PostgreSQL 的 Azure 数据库服务器时提供的服务器管理员登录用户名。The server admin login username that you supplied when you created the Azure Database for PostgreSQL server earlier. 如果不记得用户名,请按上一部分的步骤操作,以便获取连接信息。If you don't remember the username, follow the steps in the previous section to get the connection information. 格式为 username@servername。The format is username@servername.
    密码Password 管理员密码Your admin password 之前在此快速入门中创建服务器时选择的密码。The password you chose when you created the server earlier in this Quickstart.
    角色Role 留空Leave blank 此时无需提供角色名称。There's no need to provide a role name at this point. 此字段留空。Leave the field blank.
    SSL 模式SSL mode 必需Require 可以在 pgAdmin 的 SSL 选项卡中设置 SSL 模式。默认情况下,所有 Azure Database for PostgreSQL 服务器在创建时都会启用“SSL 强制实施”。You can set the SSL mode in pgAdmin's SSL tab. By default, all Azure Database for PostgreSQL servers are created with SSL enforcing turned on. 若要关闭“SSL 强制实施”,请参阅 SSL 强制实施To turn off SSL enforcing, see SSL Enforcing.
  5. 选择“其他安全性验证” 。Select Save.

  6. 在左侧的“浏览器”窗格中,展开“服务器”节点。In the Browser pane on the left, expand the Servers node. 选择服务器,例如 mydemoserverSelect your server, for example, mydemoserver. 单击该服务器与它建立连接。Click to connect to it.

  7. 展开服务器节点,然后展开其下的“数据库”。Expand the server node, and then expand Databases under it. 此列表应包括现有 postgres 数据库和已创建的任何其他数据库。The list should include your existing postgres database and any other databases you've created. 可以使用 Azure Database for PostgreSQL 为每个服务器创建多个数据库。You can create multiple databases per server with Azure Database for PostgreSQL.

  8. 右键单击“数据库”,选择“创建”菜单,并选择“数据库”。Right-click Databases, choose the Create menu, and then select Database.

  9. 在“数据库”字段中键入所选的数据库名称,例如“mypgsqldb2”。Type a database name of your choice in the Database field, such as mypgsqldb2.

  10. 从列表框中选择数据库的“所有者”。Select the Owner for the database from the list box. 选择服务器管理员登录名,例如“my admin”。Choose your server admin login name, such as the example, my admin.

    在 pgadmin 中创建数据库

  11. 选择“保存”创建新的空白数据库。Select Save to create a new blank database.

  12. 在“浏览器”窗格中,可以在服务器名称下的数据库列表中看到创建的数据库。In the Browser pane, you can see the database that you created in the list of databases under your server name.

清理资源Clean up resources

可以通过删除 Azure 资源组来清除在此快速入门中创建的所有资源。Clean up all resources you created in the quickstart by deleting the Azure resource group.

Tip

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算继续使用后续的快速入门,请不要清除在本快速入门中创建的资源。If you plan to continue to work with subsequent quickstarts, do not clean up the resources created in this quickstart. 如果不打算继续,请在 Azure CLI 中执行以下步骤,删除通过此快速入门创建的所有资源。If you do not plan to continue, use the following steps to delete all resources created by this quickstart in the Azure CLI.

az group delete --name myresourcegroup

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

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

后续步骤Next steps