快速入門:在 Azure 入口網站中建立 Azure Database for PostgreSQL 伺服器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 入口網站,在大約五分鐘內建立適用於 PostgreSQL 的 Azure 資料庫伺服器。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 free 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.

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

「適用於 PostgreSQL 的 Azure 資料庫」伺服器是以一組已設定的計算和儲存體資源所建立。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.

若要建立 Azure Database for PostgreSQL 伺服器,請採取下列步驟: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.

    功能表中的 [適用於 PostgreSQL 的 Azure 資料庫]

  3. 選取 [單一伺服器] 部署選項。Select the Single server deployment option.

    選取 [適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器] 部署選項

  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 可識別 Azure Database for PostgreSQL 伺服器的唯一名稱。A unique name that identifies your Azure Database for PostgreSQL server. 網域名稱 postgres.database.azure.com 已附加至您提供的伺服器名稱。The domain name postgres.database.azure.com 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 NoneNone 選取 [無] 從頭建立新的伺服器。Select None to create a new server from scratch. (如果您要從現有「適用於 PostgreSQL 的 Azure 資料庫」伺服器的異地備份建立伺服器,可以選取 [備份] )。(You would select Backup if you were creating a server from a geo-backup of an existing Azure Database for PostgreSQL server).
    系統管理員使用者名稱Admin username myadmin myadmin 連線至伺服器時所要使用之自己的登入帳戶。Your own login account to use when you connect to the server. 管理員登入名稱不能是 azure_superuserazure_pg_admin admin administrator root guestpublicThe 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 一般用途Gen 52 個虛擬核心5 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. 接下來,選取 [一般用途] 索引標籤。Gen 5 、4 個虛擬核心 、100 GB 和 7 天 是 [計算世代] 、[虛擬核心] 、[儲存體] 和 [備份保留期限] 的預設值。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.

    注意

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

Azure Database for PostgreSQL 會在伺服器層級建立防火牆。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.

    注意

    您的 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

當您建立 Azure Database for PostgreSQL 伺服器時,系統會建立名為 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. 若未這麼做,您可以在 Azure 入口網站的伺服器 [概觀] 頁面輕鬆尋找伺服器名稱和登入資訊。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 命令以連線到「適用於 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
    

    提示

    如果您想要使用 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.azure.com:5432/postgres
    

    連線之後,psql 公用程式會顯示 postgres 提示字元,供您鍵入 sql 命令。After you connect, the psql utility displays a postgres prompt where you type sql commands. 在初始連線輸出中,可能會因為您所使用的 psql 版本與適用於 PostgreSQL 的 Azure 資料庫伺服器版本不同,而顯示警告。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=> 
    

    提示

    如果未將防火牆設定為允許用戶端的 IP 位址,就會發生下列錯誤:If the firewall is not configured to allow the IP address of your client, the following error occurs:

    "psql:FATAL: no pg_hba.conf entry for host <IP address>, user "myadmin", database "postgres", SSL on FATAL:需要 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 連線至適用於 PostgreSQL 的 Azure 資料庫伺服器,並已建立空的使用者資料庫。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 您稍早建立 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

您有兩種方式可以清除您在本快速入門中建立的資源。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.

提示

此集合中的其他快速入門會以本快速入門為基礎。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. 然後選取您的資源群組名稱,例如範例中的 myresourcegroupThen 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. 在文字方塊中輸入您的伺服器名稱,例如範例中的 mydemoserverType your server name in the text box, such as the example, mydemoserver. 選取 [刪除] 。Select Delete.

後續步驟Next steps