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

本快速入门介绍如何使用 PowerShell 在 Azure 资源组中创建 Azure Database for PostgreSQL 服务器。This quickstart describes how to use PowerShell to create an Azure Database for PostgreSQL server in an Azure resource group. 可以使用 PowerShell 以交互方式或者通过脚本创建和管理 Azure 资源。You can use PowerShell to create and manage Azure resources interactively or in scripts.

先决条件Prerequisites

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

本文要求安装 Az PowerShell 模块,并使用 Connect-AzAccount cmdlet 连接到 Azure 帐户。This article requires that you install the Az PowerShell module and connect to your Azure account using the Connect-AzAccount cmdlet. 有关安装 Az PowerShell 模块的详细信息,请参阅安装 Azure PowerShellFor more information about installing the Az PowerShell module, see Install Azure PowerShell.

重要

尽管 Az.PostgreSql PowerShell 模块为预览版,但必须使用以下命令从 Az PowerShell 模块单独安装它:Install-Module -Name Az.PostgreSql -AllowPrereleaseWhile the Az.PostgreSql PowerShell module is in preview, you must install it separately from the Az PowerShell module using the following command: Install-Module -Name Az.PostgreSql -AllowPrerelease.

如果这是你第一次使用 Azure Database for PostgreSQL 服务,必须注册 Microsoft.DBforPostgreSQL 资源提供程序。If this is your first time using the Azure Database for PostgreSQL service, you must register the Microsoft.DBforPostgreSQL resource provider.

Register-AzResourceProvider -ProviderNamespace Microsoft.DBforPostgreSQL

如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. 使用 Set-AzContext cmdlet 选择特定的订阅 ID。Select a specific subscription ID using the Set-AzContext cmdlet.

Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000

创建资源组Create a resource group

使用 New-AzResourceGroup cmdlet 创建 Azure 资源组Create an Azure resource group using the New-AzResourceGroup cmdlet. 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。A resource group is a logical container in which Azure resources are deployed and managed as a group.

以下示例在“中国北部 2”区域中创建名为“myresourcegroup”的资源组。The following example creates a resource group named myresourcegroup in the China North 2 region.

New-AzResourceGroup -Name myresourcegroup -Location chinanorth2

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

使用 New-AzPostgreSqlServer cmdlet 创建 Azure Database for PostgreSQL 服务器。Create an Azure Database for PostgreSQL server with the New-AzPostgreSqlServer cmdlet. 一个服务器可以管理多个数据库。A server can manage multiple databases. 通常,每个项目或每个用户使用一个单独的数据库。Typically, a separate database is used for each project or for each user.

下表列出了 New-AzPostgreSqlServer cmdlet 的常用参数和示例值。The following table contains a list of commonly used parameters and sample values for the New-AzPostgreSqlServer cmdlet.

设置Setting 示例值Sample value 说明Description
名称Name mydemoservermydemoserver 选择用于标识 Azure Database for PostgreSQL 服务器的在 Azure 中全局唯一的名称。Choose a globally unique name in Azure that identifies your Azure Database for PostgreSQL server. 该服务器名称只能包含字母、数字和连字符 (-)。The server name can only contain letters, numbers, and the hyphen (-) character. 在创建过程中,指定的任何大写字符将自动转换为小写。Any uppercase characters that are specified are automatically converted to lowercase during the creation process. 必须包含 3 到 63 个字符。It must contain from 3 to 63 characters.
ResourceGroupNameResourceGroupName myresourcegroupmyresourcegroup 提供 Azure 资源组的名称。Provide the name of the Azure resource group.
SKUSku 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. 有关 SKU 参数的详细信息,请参阅此表格后面的信息。For more information about the Sku parameter, see the information following this table.
BackupRetentionDayBackupRetentionDay 77 备份保留时间。How long a backup should be retained. 单位为天。Unit is days. 范围为 7-35。Range is 7-35.
GeoRedundantBackupGeoRedundantBackup EnabledEnabled 是否应为此服务器启用异地冗余备份。Whether geo-redundant backups should be enabled for this server or not. 不能为基本定价层级中的服务器启用此值,并且在创建服务器后无法更改此值。This value cannot be enabled for servers in the basic pricing tier and it cannot be changed after the server is created. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
位置Location chinanorth2chinanorth2 服务器的 Azure 区域。The Azure region for the server.
SslEnforcementSslEnforcement EnabledEnabled 是否应该为此服务器启用 SSL。Whether SSL should be enabled or not for this server. 允许的值:“Enabled”、“Disabled”。Allowed values: Enabled, Disabled.
StorageInMbStorageInMb 5120051200 服务器的存储容量(以 MB 为单位)。The storage capacity of the server (unit is megabytes). 有效的 StorageInMb 最小为 5120 MB,以 1024 MB 为增量递增。Valid StorageInMb is a minimum of 5120 MB and increases in 1024 MB increments. 有关存储大小限制的详细信息,请参阅 Azure Database for PostgreSQL 定价层For more information about storage size limits, see Azure Database for PostgreSQL pricing tiers.
版本Version 9.69.6 PostgreSQL 主版本。The PostgreSQL major version.
AdministratorUserNameAdministratorUserName myadminmyadmin 管理员的登录用户名。The username for the administrator login. 不能是 azure_superuseradminadministratorrootguestpublicIt cannot be azure_superuser, admin, administrator, root, guest, or public.
AdministratorLoginPasswordAdministratorLoginPassword <securestring> 管理员用户的密码,采用安全字符串格式。The password of the administrator user in the form of a secure string. 该密码必须包含 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 参数值遵循 pricing-tier_compute-generation_vCores 约定,如以下示例所示 。The Sku parameter value follows the convention pricing-tier_compute-generation_vCores as shown in the following examples.

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

有关各区域和层级的有效 Sku 值的信息,请参阅 Azure Database for PostgreSQL 定价层For information about valid Sku values by region and for tiers, see Azure Database for PostgreSQL pricing tiers.

以下示例使用服务器管理员登录名“myadmin”在“中国北部 2”区域中的“myresourcegroup”资源组内创建名为“mydemoserver”的 PostgreSQL 服务器 。The following example creates a PostgreSQL server in the China North 2 region named mydemoserver in the myresourcegroup resource group with a server admin login of myadmin. 此服务器是常规用途定价层中的第 5 代服务器,其中启用了 2 个 vCore 和异地冗余备份。It is a Gen 5 server in the general-purpose pricing tier with 2 vCores and geo-redundant backups enabled. 记下示例的第一行中使用的密码,因为这是 PostgreSQL 服务器管理员帐户的密码。Document the password used in the first line of the example as this is the password for the PostgreSQL server admin account.

提示

服务器名称映射到 DNS 名称,必须在 Azure 中全局唯一。A server name maps to a DNS name and must be globally unique in Azure.

$Password = Read-Host -Prompt 'Please enter your password' -AsSecureString
New-AzPostgreSqlServer -Name mydemoserver -ResourceGroupName myresourcegroup -Sku GP_Gen5_2 -GeoRedundantBackup Enabled -Location chinanorth2 -AdministratorUsername myadmin -AdministratorLoginPassword $Password

如果轻量级计算和 I/O 足以满足工作负荷要求,请考虑使用基本定价层级。Consider using the basic pricing tier if light compute and I/O are adequate for your workload.

重要

在基本定价层中创建的服务器以后无法扩展到常规用途或内存优化层级,并且无法异地复制。Servers created in the basic pricing tier cannot be later scaled to general-purpose or memory- optimized and cannot be geo-replicated.

配置防火墙规则Configure a firewall rule

使用 New-AzPostgreSqlFirewallRule cmdlet 创建 Azure Database for PostgreSQL 服务器级防火墙规则。Create an Azure Database for PostgreSQL server-level firewall rule using the New-AzPostgreSqlFirewallRule cmdlet. 服务器级防火墙规则允许外部应用程序(如 psql 命令行工具或 PostgreSQL Workbench)穿过 Azure Database for PostgreSQL 服务防火墙连接到服务器。A server-level firewall rule allows an external application, such as the psql command-line tool or PostgreSQL Workbench to connect to your server through the Azure Database for PostgreSQL service firewall.

以下示例创建名为 AllowMyIP 的防火墙规则,该规则允许从特定的 IP 地址 (192.168.0.1) 进行连接。The following example creates a firewall rule named AllowMyIP that allows connections from a specific IP address, 192.168.0.1. 替换与要从中进行连接的位置相对应的 IP 地址或 IP 地址范围。Substitute an IP address or range of IP addresses that correspond to the location that you are connecting from.

New-AzPostgreSqlFirewallRule -Name AllowMyIP -ResourceGroupName myresourcegroup -ServerName mydemoserver -StartIPAddress 192.168.0.1 -EndIPAddress 192.168.0.1

备注

连接到 Azure Database for PostgreSQL 时,经端口 5432 进行通信。Connections to Azure Database for PostgreSQL communicate over port 5432. 如果尝试从企业网络内部进行连接,则可能不允许经端口 5432 的出站流量。If you try to connect from within a corporate network, outbound traffic over port 5432 might not be allowed. 这种情况下,只有在 IT 部门打开了端口 5432 时,才能连接到服务器。In this scenario, you can only connect to the server if your IT department opens port 5432.

获取连接信息Get the connection information

若要连接到服务器,需要提供主机信息和访问凭据。To connect to your server, you need to provide host information and access credentials. 使用以下示例来确定连接信息。Use the following example to determine the connection information. 记下 FullyQualifiedDomainName 和 AdministratorLogin 的值。Make a note of the values for FullyQualifiedDomainName and AdministratorLogin.

Get-AzPostgreSqlServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  Select-Object -Property FullyQualifiedDomainName, AdministratorLogin
FullyQualifiedDomainName                    AdministratorLogin
------------------------                    ------------------
mydemoserver.postgres.database.chinacloudapi.cn       myadmin

使用 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.

  1. 使用 psql 命令行实用工具连接到 Azure PostgreSQL 服务器。Connect to your Azure PostgreSQL server using the psql command-line utility.

    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
    

    提示

    如果更喜欢使用 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 的连接字符串将是 psql postgresql://myadmin%40mydemoserver@mydemoserver.postgres.database.chinacloudapi.cn:5432/postgresFor example the connection string for psql would be, psql postgresql://myadmin%40mydemoserver@mydemoserver.postgres.database.chinacloudapi.cn:5432/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 选项卡中设置 TLS/SSL 模式。默认情况下,所有 Azure Database for PostgreSQL 服务器在创建时都会启用“强制实施 TLS”。You can set the TLS/SSL mode in pgAdmin's SSL tab. By default, all Azure Database for PostgreSQL servers are created with TLS enforcing turned on. 若要禁用“强制实施 TLS”,请参阅配置 TLS 的强制实施To turn off TLS enforcing, see Configure Enforcement of TLS.
  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

如果其他快速入门或教程不需要使用本快速入门中创建的资源,可以运行以下示例将其删除。If the resources created in this quickstart aren't needed for another quickstart or tutorial, you can delete them by running the following example.

注意

以下示例删除指定的资源组及其包含的所有资源。The following example deletes the specified resource group and all resources contained within it. 如果指定的资源组中存在本快速入门范围外的资源,这些资源也会被删除。If resources outside the scope of this quickstart exist in the specified resource group, they will also be deleted.

Remove-AzResourceGroup -Name myresourcegroup

若要仅删除本快速入门中创建的服务器而不删除资源组,请使用 Remove-AzPostgreSqlServer cmdlet。To delete only the server created in this quickstart without deleting the resource group, use the Remove-AzPostgreSqlServer cmdlet.

Remove-AzPostgreSqlServer -Name mydemoserver -ResourceGroupName myresourcegroup

后续步骤Next steps