快速入门:在 Azure 门户中创建用于 PostgreSQL 的 Azure 数据库服务器Quickstart: Create an Azure Database for PostgreSQL server in the Azure portal

Azure Database for PostgreSQL 数据库是一种托管服务,可用于在云中运行、管理和缩放具有高可用性的 PostgreSQL 数据库。Azure Database for PostgreSQL is a managed service that you use to run, manage, and scale highly available PostgreSQL databases in the cloud. 本快速入门介绍如何使用 Azure 门户在大约五分钟内创建 Azure Database for PostgreSQL 服务器。This Quickstart shows you how to create an Azure Database for PostgreSQL server in about five minutes using the Azure portal.

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

登录到 Azure 门户Sign in to the Azure portal

打开 Web 浏览器并转到门户Open your web browser and go to the portal. 输入登录到门户所需的凭据。Enter your credentials to sign in to the portal. 默认视图是服务仪表板。The default view is your service dashboard.

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

创建的 Azure Database for PostgreSQL 服务器中包含一组已配置的计算和存储资源An Azure Database for PostgreSQL server is created with a configured set of compute and storage resources. 将在 Azure 资源组中创建服务器。The server is created within an Azure resource group.

若要创建用于 PostgreSQL 的 Azure 数据库服务器,请执行以下步骤:To create an Azure Database for PostgreSQL server, take the following steps:

  1. 选择门户左上角的“创建资源”按钮 (+) 。Select the Create a resource button (+) in the upper-left corner of the portal.

  2. 选择“数据库” > “Azure Database for PostgreSQL”。Select Databases > Azure Database for PostgreSQL.

    菜单中的“Azure Database for PostgreSQL”

  3. 选择“单个服务器”部署选项 。Select the Single server deployment option.

    选择 Azure Database for PostgreSQL - 单个服务器部署选项

  4. 填写“基本”表单,其中包含以下信息 :Fill out the Basics form with the following information:

    创建服务器

    设置Setting 建议的值Suggested Value 说明Description
    订阅Subscription 订阅名称Your subscription name 要用于服务器的 Azure 订阅。The Azure subscription that you want to use for your server. 如果有多个订阅,请选择要计费的资源所在的订阅。If you have multiple subscriptions, choose the subscription in which you're billed for the resource.
    资源组Resource group myresourcegroupmyresourcegroup 新的资源组名称,或订阅中的现有资源组。A new resource group name or an existing one from your subscription.
    服务器名称Server name mydemoservermydemoserver 用于标识用于 PostgreSQL 的 Azure 数据库服务器的唯一名称。A unique name that identifies your Azure Database for PostgreSQL server. 域名 postgres.database.chinacloudapi.cn 将追加到所提供的服务器名称后面。The domain name postgres.database.chinacloudapi.cn is appended to the server name you provide. 服务器名称只能包含小写字母、数字和连字符 (-) 字符。The server can contain only lowercase letters, numbers, and the hyphen (-) character. 该名称必须至少包含 3 到 63 个字符。It must contain at least 3 through 63 characters.
    数据源Data source None 选择“无”,从头开始创建新的服务器 。Select None to create a new server from scratch. (如果是从现有 Azure Database for PostgreSQL 服务器的异地备份创建服务器,则会选择“备份” )。(You would select Backup if you were creating a server from a geo-backup of an existing Azure Database for PostgreSQL server).
    管理员用户名Admin username myadminmyadmin 连接到服务器时使用的自己的登录帐户。Your own login account to use when you connect to the server. 管理员登录名不能是 azure_superuserazure_pg_adminadminadministratorrootguestpublicThe admin login name can't be azure_superuser, azure_pg_admin, admin, administrator, root, guest, or public. 不能以 pg_ 开头。It can't start with pg_.
    密码Password 你的密码Your password 服务器管理员帐户的新密码。A new password for the server admin account. 该密码必须包含 8 到 128 个字符。It must contain between 8 and 128 characters. 密码必须包含以下三个类别的字符:英文大写字母、英文小写字母、数字 (0 到 9)和非字母数字字符(!, $, #, % 等)。Your password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers (0 through 9), and non-alphanumeric characters (!, $, #, %, etc.).
    位置Location 离用户最近的区域The region closest to your users 最靠近用户的位置。The location that is closest to your users.
    版本Version 最新主版本The latest major version 除非另有特定的要求,否则为最新 PostgreSQL 主版本。The latest PostgreSQL major version, unless you have specific requirements otherwise.
    计算 + 存储Compute + storage 常规用途第 5 代2 vCore5 GB7 天异地冗余General Purpose, Gen 5, 2 vCores, 5 GB, 7 days, Geographically Redundant 新服务器的计算、存储和备份配置。The compute, storage, and backup configurations for your new server. 选择“配置服务器” 。Select Configure server. 接下来,选择“常规用途” 选项卡。“第 5 代”、“4 个 vCore”、“100 GB”和“7 天”分别是“计算代系”、“vCore”、“存储”和“备份保持期”的默认值 。Next, select the General Purpose tab. Gen 5, 4 vCores, 100 GB, and 7 days are the default values for Compute Generation, vCore, Storage, and Backup Retention Period. 可以按原样保留这些滑块,也可以对其进行调整。You can leave those sliders as is or adjust them. 若要在异地冗余存储中启用服务器备份,请从“备份冗余选项” 中选择“异地冗余” 。To enable your server backups in geo-redundant storage select Geographically Redundant from the Backup Redundancy Options. 若要保存此定价层选择,请选择“确定” 。To save this pricing tier selection, select OK. 下一个屏幕截图捕获了这些选择。The next screenshot captures these selections.

    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.

    “定价层”窗格

  5. 选择“查看 + 创建”,查看所选内容 。Select Review + create to review your selections. 选择“创建” 以预配服务器。Select Create to provision the server. 此操作可能需要几分钟的时间。This operation may take a few minutes.

  6. 在工具栏上选择“通知”图标(铃铛)以监视部署过程。 On the toolbar, select the Notifications icon (a bell) to monitor the deployment process. 完成部署后,可以选择“固定到仪表板” ,以便在 Azure 门户仪表板上为此服务器创建磁贴作为到此服务器“概述” 页的快捷方式。Once the deployment is done, you can select Pin to dashboard, which creates a tile for this server on your Azure portal dashboard as a shortcut to the server's Overview page. 选择“转到资源” 可打开此服务器的“概述” 页。Selecting Go to resource opens the server's Overview page.

    “通知”窗格

    默认情况下,会在服务器下创建 postgres 数据库。By default, a postgres database is created under your server. postgres 是供用户、实用工具和第三方应用程序使用的默认数据库。The postgres database is a default database that's meant for use by users, utilities, and third-party applications. (另一个默认数据库是 azure_maintenance(The other default database is azure_maintenance. 其功能是将托管服务进程与用户操作分开。Its function is to separate the managed service processes from user actions. 你不能访问此数据库。)You cannot access this database.)

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

用于 PostgreSQL 的 Azure 数据库会在服务器级别创建防火墙。Azure Database for PostgreSQL creates a firewall at the server level. 除非创建了规则来为特定的 IP 地址打开防火墙,否则此防火墙会阻止外部应用程序和工具连接到服务器和服务器上的任何数据库。It prevents external applications and tools from connecting to the server and any databases on the server, unless you create a rule to open the firewall for specific IP addresses.

  1. 部署完成后,找到服务器。After the deployment finishes, locate your server. 可以根据需要进行搜索。If needed, you can search for it. 例如,在左侧菜单中选择“所有资源”。 For example, on the menu on the left, select All resources. 键入服务器名称(例如 mydemoserver)来搜索新建的服务器。Type your server name, such as the example, mydemoserver, to search for your newly created server. 从搜索结果列表中选择服务器名称。Select your server name from the search result list. 服务器的“概述” 页面随即打开,其中提供了用于进一步配置的选项。The Overview page for your server opens and provides options for further configuration.

    服务器名称搜索

  2. 在服务器页中,选择“连接安全性” 。On the server page, select Connection security.

  3. 在“防火墙规则”下的“规则名称”列中选择空白文本框,开始创建防火墙规则。 Under the Firewall rules, in the Rule Name column, select the blank text box to begin creating the firewall rule.

    在文本框中填写将访问服务器的客户端的名称,以及起始和结束 IP 范围。Fill in the text boxes with a name, and the start and end IP range of the clients that will be accessing your server. 如果它是单个 IP,请为起始 IP 和结束 IP 使用相同的值。If it is a single IP, use the same value for the start IP and end IP.

    设置防火墙规则

  4. 在“连接安全性”页的上部工具栏中,选择“保存”。 On the upper toolbar of the Connection security page, select Save. 等到指示连接安全性更新已成功完成的通知出现后,再继续操作。Wait until the notification appears stating that the connection security update has finished successfully before you continue.

    Note

    连接到 Azure Database for PostgreSQL 服务器时,通过端口 5432 进行通信。Connections to your Azure Database for PostgreSQL server communicate over port 5432. 如果尝试从企业网络内部进行连接,则该网络的防火墙可能不允许经端口 5432 的出站流量。When you try to connect from within a corporate network, outbound traffic over port 5432 might not be allowed by your network's firewall. 如果是这样,则无法连接到服务器,除非 IT 部门打开了端口 5432。If so, you can't connect to your server unless your IT department opens port 5432.

获取连接信息Get the connection information

创建用于 PostgreSQL 的 Azure 数据库服务器时,会创建名为 postgres 的默认数据库。When you create your Azure Database for PostgreSQL server, a default database named postgres is created. 若要连接到数据库服务器,需要完整的服务器名称和管理员登录凭据。To connect to your database server, you need your full server name and admin login credentials. 此前可能已在本快速入门文章中记下这些值。You might have noted those values earlier in the Quickstart article. 如果没有记下这些值,可以在门户的服务器“概览”页中轻松找到服务器名称和登录信息。 If you didn't, you can easily find the server name and login information on the server Overview page in the portal.

打开服务器的“概览” 页。Open your server's Overview page. 记下“服务器名称” 和“服务器管理员登录名” 。Make a note of the Server name and the Server admin login name. 将光标悬停在每个字段,然后复制符号就会显示在文本右侧。Hover your cursor over each field, and the copy symbol appears to the right of the text. 根据需要选择复制符号即可复制这些值。Select the copy symbol as needed to copy the values.

服务器“概述”页

使用 psql 连接到 PostgreSQL 数据库Connect to the PostgreSQL database using psql

可以通过多个应用程序连接到 Azure Database for PostgreSQL 服务器。There are a number of applications you can use to connect to your Azure Database for PostgreSQL server. 如果客户端计算机已安装 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
    

    Tip

    如果更喜欢使用 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
    

    连接后,psql 实用工具会显示 postgres 提示符,要求在其中键入 sql 命令。After you connect, the psql utility displays a postgres prompt where you type sql commands. 在初始连接输出中可能会显示警告,因为所使用的 psql 版本可能不同于 Azure Database for PostgreSQL 服务器版本。In the initial connection output, a warning may appear because the psql you're using might be a different version than the Azure Database for PostgreSQL server version.

    psql 输出示例:Example psql output:

    psql (9.5.7, server 9.6.2)
    WARNING: psql major version 9.5, server major version 9.6.
     Some psql features might not work.
     SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-SHA384, bits: 256, compression: off)
    Type "help" for help.
    
    postgres=> 
    

    Tip

    如果未将防火墙配置为允许客户端的 IP 地址,则会出现以下错误:If the firewall is not configured to allow the IP address of your client, the following error occurs:

    psql: 致命错误: 主机 <IP address>、用户 "myadmin"、数据库 "postgres" 没有 pg_hba.conf 条目,SSL 出现致命错误:需要 SSL 连接。"psql: FATAL: no pg_hba.conf entry for host <IP address>, user "myadmin", database "postgres", SSL on FATAL: SSL connection is required. 请指定 SSL 选项,然后重试。Specify SSL options and retry.

    确认在上面的防火墙规则步骤中允许你的客户端 IP。Confirm your client's IP is allowed in the firewall rules step above.

  2. 出现提示时通过键入以下命令来创建名为“mypgsqldb”的空数据库:Create a blank database called "mypgsqldb" at the prompt by typing the following command:

    CREATE DATABASE mypgsqldb;
    
  3. 在提示符下,执行以下命令来将连接切换到新建的数据库 mypgsqldbAt the prompt, execute the following command to switch connections to the newly created database mypgsqldb:

    \c mypgsqldb
    
  4. 键入 \q,再按 Enter 键退出 psql。Type \q, and then select the Enter key to quit psql.

已通过 psql 连接到 Azure Database for PostgreSQL 服务器并创建了一个空用户数据库。You connected to the Azure Database for PostgreSQL server via psql, and you created a blank user database. 请转到下一部分,使用另一常用工具 pgAdmin 进行连接。Continue to the next section to connect by using another common tool, pgAdmin.

使用 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 Value 说明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

可以通过两种方法之一清理在快速入门中创建的资源。You can clean up the resources that you created in the Quickstart in one of two ways. 可以删除 Azure 资源组,其中包括资源组中的所有资源。You can delete the Azure resource group, which includes all the resources in the resource group. 若要保持其他资源原封不动,请只删除服务器资源。If you want to keep the other resources intact, delete only the server resource.

Tip

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other Quickstarts in this collection build on this Quickstart. 如果打算继续使用快速入门,请不要清除在本快速入门中创建的资源。If you plan to continue working with Quickstarts, don't clean up the resources that you created in this Quickstart. 如果不打算继续,请在门户中执行以下步骤来删除本快速入门创建的资源。If you don't plan to continue, follow these steps to delete the resources that were created by this Quickstart in the portal.

若要删除包括新建服务器在内的整个资源组,请执行以下操作:To delete the entire resource group, including the newly created server:

  1. 在门户中找到自己的资源组。Locate your resource group in the portal. 在左侧菜单中,选择“资源组” 。On the menu on the left, select Resource groups. 然后选择资源组的名称,例如 myresourcegroup 。Then select the name of your resource group, such as the example, myresourcegroup.

  2. 在资源组页上,选择“删除” 。On your resource group page, select Delete. 在文本框中键入确认删除的资源组的名称,例如 myresourcegroup 。Type the name of your resource group, such as the example, myresourcegroup, in the text box to confirm deletion. 选择“删除” 。Select Delete.

仅删除新建的服务器:To delete only the newly created server:

  1. 在门户中找到自己的服务器(如果尚未将其打开)。Locate your server in the portal, if you don't have it open. 在左侧菜单中选择“所有资源”。 On the menu on the left, select All resources. 然后搜索所创建的服务器。Then search for the server you created.

  2. 在“概览” 页上,选择“删除” 。On the Overview page, select Delete.

    “删除”按钮

  3. 确认要删除的服务器名称并查看其下受影响的数据库。Confirm the name of the server you want to delete, and view the databases under it that are affected. 在文本框中键入服务器名称,例如 mydemoserver 。Type your server name in the text box, such as the example, mydemoserver. 选择“删除” 。Select Delete.

后续步骤Next steps