使用 Google Cloud 控制台建立及查詢 AlloyDB 資料庫

在 AlloyDB for PostgreSQL 中,資料庫會儲存在包含執行個體叢集中。在本快速入門導覽課程中,您將設定叢集、連線至叢集執行個體,並插入及查詢範例資料。如要進一步瞭解 AlloyDB 架構、叢集和執行個體,請參閱 AlloyDB 總覽

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  9. Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

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

    如要使用位於不同 Google Cloud 專案的虛擬私有雲網路,設定 AlloyDB 的網路連線,則必須使用 Compute Engine API 和 Cloud Resource Manager API。

  11. 選擇連線至 AlloyDB 的方式

必要的角色

如要取得建立及連線至 AlloyDB 資料庫所需的權限,請要求管理員授予您專案的下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這些預先定義的角色具備建立及連線至 AlloyDB 資料庫所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

如要建立及連線至 AlloyDB 資料庫,您必須具備下列權限:

  • compute.networks.list
  • compute.networks.addPeering
  • compute.addresses.create
  • servicenetworking.services.addPeering
  • alloydb.clusters.create
  • alloydb.instances.create
  • alloydb.instances.login

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

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

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

    前往「Clusters」(叢集) 頁面

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

  3. 在「設定叢集」中,按照下列步驟操作:

    1. 在「Cluster ID」(叢集 ID) 欄位中輸入 my-cluster

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

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

    4. 在「Location」(位置) 的「Region」(區域) 下拉式選單中,選取「us-central1 (Iowa)」區域。

  4. 在「設定主要執行個體」中,按照下列步驟操作:

    1. 在「Instance ID」(執行個體 ID) 欄位中輸入 my-cluster-primary

    2. 在「可用區可用性」部分,按一下「單一可用區」

    3. 在「Machine type」(機器類型) 下拉式選單中,選取「2 vCPU, 16 GB」機器類型。

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

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

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

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

    5. 保留「網路安全性」下的預設設定。

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

連線至執行個體,並在 AlloyDB Studio 中建立資料庫

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

    前往「Clusters」(叢集) 頁面

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

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

  4. 在「登入 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. 在「登入 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 控制台的「網路」頁面,然後按一下「刪除虛擬私有雲網路」

後續步驟