快速入門:使用 Azure CLI 建立適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器Quickstart: Create an Azure Database for PostgreSQL - Single Server using the Azure CLI

提示

請考慮使用較簡單的 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 free account before you begin.

開啟 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,並且會透過瀏覽器來使用。Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell 可讓您使用 bashPowerShell 殼層來執行各種可與 Azure 服務搭配運作的工具。Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文的內容,而不必在本機環境上安裝任何工具。Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

若要在 Azure Cloud Shell 上執行本文所包含的任何程式碼,請開啟 Cloud Shell 工作階段、使用某個程式碼區塊上的 [複製] 按鈕來複製程式碼,然後使用 Ctrl+Shift+V (在 Windows 和 Linux 上) 或 Cmd+Shift+V (在 macOS 上) 將程式碼貼到 Cloud Shell 工作階段中。To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. 貼上的文字不會自動執行,因此請按 Enter 來執行程式碼。Pasted text is not automatically executed, so press Enter to run code.

您可以使用下列方式來啟動 Azure Cloud Shell:You can launch Azure Cloud Shell with:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 這__不會__自動將文字複製到 Cloud Shell。This doesn't automatically copy text to Cloud Shell. Azure Cloud Shell 的試試看範例
在瀏覽器中開啟 Azure Cloud ShellOpen Azure Cloud Shell in your browser. <a href="https://shell.azure.com" title="啟動 Azure Cloud Shell
選取 Azure 入口網站右上角功能表上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

如果您選擇在本機安裝和使用 CLI,本文會要求您執行 Azure CLI 2.0 版或更新版本。If you choose to install and use the CLI locally, 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. 請記下命令輸出中的識別碼屬性,以取得對應的訂用帳戶名稱。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 輸出中的識別碼屬性,替代訂用帳戶識別碼的預留位置。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. 下列範例會在 westus 位置建立名為 myresourcegroup 的資源群組。The following example creates a resource group named myresourcegroup in the westus location.

az group create --name myresourcegroup --location westus

建立適用於 PostgreSQL 的 Azure 資料庫伺服器Create an Azure Database for PostgreSQL server

使用 az postgres server create 命令來建立適用於 PostgreSQL 的 Azure 資料庫伺服器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_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-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 westuswestus 伺服器的 Azure 位置。The Azure location for the server.
ssl-enforcementssl-enforcement 已啟用Enabled 是否應針對此伺服器啟用 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 的最小值為 5120 MB,並以 1024 MB 的增量增加。Valid storage-size is minimum 5120MB and increases in 1024MB increments. 如需儲存體大小限制的詳細資訊,請參閱定價層文件。See the pricing tiers document for more information about storage size limits.
versionversion 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 參數值會遵循慣例 {pricing tier}_{compute generation}_{vCores},如下列範例所示:The sku-name parameter value follows the convention {pricing tier}_{compute generation}_{vCores} as in the examples below:

  • --sku-name B_Gen5_1 對應於基本、第 5 代和 1 個虛擬核心。--sku-name B_Gen5_1 maps to Basic, Gen 5, 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.

下列範例會使用伺服器系統管理員登入 myadminmyresourcegroup 您的資源群組中建立且名為 mydemoserver 的 PostgreSQL 9.6 伺服器 (位於美國西部)。The following example creates a PostgreSQL 9.6 server in West US named mydemoserver in your resource group myresourcegroup with server admin login myadmin. 這是第 4 代 一般用途伺服器,它具有 2 個 vCoreThis 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 westus --admin-user myadmin --admin-password <server_admin_password> --sku-name GP_Gen4_2 --version 9.6

注意

如果您的工作負載只需要輕量計算和 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

注意

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. 請記下 administratorLoginfullyQualifiedDomainNameMake a note of the administratorLogin and fullyQualifiedDomainName.

{
  "administratorLogin": "myadmin",
  "earliestRestoreDate": null,
  "fullyQualifiedDomainName": "mydemoserver.postgres.database.azure.com",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforPostgreSQL/servers/mydemoserver",
  "location": "westus",
  "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 連線到 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 命令以連線到「適用於 PostgreSQL 的 Azure 資料庫」伺服器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.azure.com 上名為 postgres 的預設資料庫。For example, the following command connects to the default database called postgres on your PostgreSQL server mydemoserver.postgres.database.azure.com using access credentials. 系統提示輸入密碼時,請輸入您選擇的 <server_admin_password>Enter the <server_admin_password> you chose when prompted for password.

    psql --host=mydemoserver.postgres.database.azure.com --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. 在提示字元,執行下列命令以將連線切換到新建立的資料庫 mypgsqldbAt 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 Value 說明Description
    主機名稱/位址Host name/address 伺服器名稱Server name 您稍早建立 Azure Database for PostgreSQL 伺服器時所用的伺服器名稱值。The server name value that you used when you created the Azure Database for PostgreSQL server earlier. 我們的範例伺服器是 mydemoserver.postgres.database.azure.comOur example server is mydemoserver.postgres.database.azure.com. 使用如範例所示的完整網域名稱 (*.postgres.database.azure.com)。Use the fully qualified domain name (*.postgres.database.azure.com) 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.
    PortPort 54325432 當您連線至 Azure Database for PostgreSQL 伺服器時所要使用的連接埠。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 您稍早建立 Azure Database for PostgreSQL 時所提供的伺服器管理員登入使用者名稱。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@servernameThe 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 模式。根據預設,所有適用於 PostgreSQL 的 Azure 資料庫伺服器建立時都會開啟強制執行 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,請參閱強制執行 SSLTo 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. 您可以使用適用於 PostgreSQL 的 Azure 資料庫,為每一部伺服器建立多個資料庫。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. 在 [資料庫] 欄位中輸入您選擇的資料庫名稱,例如 mypgsqldb2Type 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 adminChoose 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.

提示

此集合中的其他快速入門會以本快速入門為基礎。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