建立及連線至資料庫

本頁將逐步說明如何建立及連線至 PostgreSQL 適用的 AlloyDB 中的第一個資料庫。

本快速入門指南結尾會提供相關操作說明,引導您清除這個新資料庫。最後,這頁面會說明後續步驟,協助您進一步瞭解如何搭配自己的應用程式使用 AlloyDB。

事前準備

主控台

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. 啟用建立及連線至 PostgreSQL 適用的 AlloyDB 時所需的 Cloud API。

    啟用 API

    1. 在「確認專案」步驟中,按一下「下一步」,確認要變更的專案名稱。

    2. 在「啟用 API」步驟中,按一下「啟用」,啟用下列項目:

      • AlloyDB API
      • Compute Engine API
      • Cloud Resource Manager API
      • Service Networking API

      如果您打算使用與 AlloyDB 位於相同 Google Cloud 專案的虛擬私有雲網路,設定 AlloyDB 的網路連線,則必須啟用 Service Networking API。

      如果您打算使用位於不同 Google Cloud 專案的 VPC 網路,設定與 AlloyDB 的網路連線,則必須使用 Compute Engine API 和 Cloud Resource Manager API。

gcloud

  1. 建立 Google Cloud 專案或使用現有專案:

    gcloud projects create PROJECT_ID --set-as-default
    # For an existing project
    gcloud config set project PROJECT_ID
    

    更改下列內容:

    • PROJECT_ID:專案的永久 ID。
  2. 請確認您已為 Google Cloud 專案啟用計費功能。瞭解如何確認專案已啟用計費功能

  3. 啟用 AlloyDB、Compute Engine 和 Resource Manager API:

    gcloud services enable alloydb.googleapis.com
    gcloud services enable compute.googleapis.com
    gcloud services enable cloudresourcemanager.googleapis.com
    
  4. 如果您打算使用與 AlloyDB 位於同一個 Google Cloud 專案的虛擬私有雲網路,設定與 AlloyDB 的網路連線,請在專案中啟用 Service Networking API。

    gcloud services enable servicenetworking.googleapis.com
    
  5. 如果您打算使用位於不同專案的虛擬私有雲網路,設定與 AlloyDB 的網路連線,請確認該專案已啟用 Compute Engine 和 Service Networking API,以便在該專案中設定共用虛擬私有雲和私有服務存取權。 Google Cloud

    gcloud services list
    

建立叢集及其主要執行個體

注意:如果您打算使用私人網路,可以透過 Terraform 部署所選的私人網路設定和 AlloyDB 執行個體,以及 Compute Engine VM 等用戶端。詳情請參閱 簡化雲端網路設定解決方案

  1. 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。

    前往「Clusters」(叢集) 頁面

  2. 按一下 [Create cluster] (建立叢集)

  3. 在「叢集 ID」中輸入 my-cluster

  4. 輸入密碼。請記下這組密碼,因為您會在本次快速入門中使用。

  5. 保留預設資料庫版本。

  6. 在「位置」中,選取「us-central1 (Iowa)」區域。

  7. 在「連線」的「網路」下拉式選單中,選取「default」網路。

    如果您有私人存取連線,請繼續下一個步驟。 如果沒有,請按一下「設定連線」,然後按照下列步驟操作:

    1. 在「Allocate an IP range」(分配 IP 範圍) 中,按一下「Use an automatically allocated IP range」(使用系統自動分配的 IP 範圍)

    2. 依序點選「繼續」和「建立連線」

  8. 保留預設執行個體 ID my-cluster-primary

  9. 在「可用區可用性」中,保留選取的「單一可用區」選項。 雖然我們不建議在正式環境中使用單一可用區執行個體,但您可以在本快速入門導覽課程中使用單一可用區,盡量降低成本。詳情請參閱「使用基本執行個體來降低成本」一文。

  10. 選取 2 vCPU, 16 GB 機型。

  11. 保留「連線」和「網路安全性」下的預設設定。

  12. 請勿在「顯示進階選項」中進行變更。

  13. 按一下「Create cluster」(建立叢集)。AlloyDB 可能需要幾分鐘才能建立叢集,並顯示在主要叢集的「總覽」頁面上。

連線至執行個體並建立資料庫

  1. 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。

    前往「Clusters」(叢集) 頁面

  2. 在「資源名稱」資料欄中,按一下叢集名稱 my-cluster

  3. 在導覽窗格中,按一下「AlloyDB Studio」

  4. 在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面,按照下列步驟操作:

    1. 選擇資料庫驗證方法

    2. 選取 postgres 資料庫。

    3. 按一下「驗證」。「Explorer」窗格會顯示 postgres 資料庫中的物件清單。

  5. 如要建立資料庫,請在「Editor 1」(編輯器 1) 分頁標籤中輸入下列陳述式,然後按一下「Run」(執行)

    CREATE DATABASE guestbook;
    

    如果「結果」窗格中顯示 Statement executed successfully 訊息,表示資料庫已建立。

連線至 guestbook 資料庫

  1. 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。

    前往「Clusters」(叢集) 頁面

  2. 在「資源名稱」資料欄中,按一下叢集名稱 my-cluster

  3. 在導覽窗格中,按一下「AlloyDB Studio」

  4. 在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面,按照下列步驟操作:

    1. 選取 guestbook 資料庫。

    2. 選取 postgres 使用者。

    3. 輸入您在「建立叢集及其主要執行個體」中建立的密碼。

    4. 按一下「驗證」。「Explorer」窗格會顯示 guestbook 資料庫中的物件清單。

驗證資料庫連線

  1. 按照「連線至 guestbook 資料庫」一文中的步驟操作。

  2. 如要將範例資料插入 guestbook 資料庫,請在「Editor 1」分頁中輸入下列內容:

    CREATE TABLE entries (guestName VARCHAR(255),
                          content VARCHAR(255),
                          entryID SERIAL PRIMARY KEY);
    INSERT INTO entries (guestName, content) values ('Alex', 'I got here!');
    INSERT INTO entries (guestName, content) values ('Kai', 'Me too!');
    
  3. 按一下「執行」。「結果」窗格中的 Statement executed successfully 訊息表示資料已插入資料庫。

  4. 如要從「編輯器 1」分頁標籤中移除文字,以便執行其他指令,請按一下「清除」

  5. 如要選取資料庫中的所有項目,請在「Editor 1」(編輯器 1) 分頁中輸入下列查詢:

    SELECT * FROM entries;
    
  6. 按一下「執行」。「Results」(結果) 窗格會顯示類似下列內容的輸出:

        guestname     | content     | entryid
        --------------+-------------+---------
        Alex          | I got here! | 1
        Kai           | Me too!     | 2
    

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

  1. 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。

    前往「Clusters」(叢集) 頁面

  2. 在「資源名稱」資料欄中,按一下叢集名稱 my-cluster

  3. 按一下 「刪除叢集」

  4. 在「Delete cluster my-cluster」(刪除叢集 my-cluster) 中輸入 my-cluster,確認要刪除叢集。

  5. 點選「刪除」。

  6. 如果您在建立叢集時建立了私人連線,請前往 Google Cloud 控制台的「網路」頁面,然後按一下「刪除虛擬私有雲網路」

後續步驟