建立及連線至資料庫
本頁將逐步說明如何建立及連線至 PostgreSQL 適用的 AlloyDB 第一個資料庫。
本快速入門指南結尾會提供相關操作說明,引導您清除這個新資料庫。最後,這頁面會說明後續步驟,協助您進一步瞭解如何搭配自己的應用程式使用 AlloyDB。
事前準備
主控台
- 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.
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
    
    
      
        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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
 啟用建立及連線至 PostgreSQL 適用的 AlloyDB 時所需的 Cloud API。 - 在「確認專案」步驟中,按一下「下一步」,確認要變更的專案名稱。 
- 在「啟用 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。 
 
- 選擇連線至 AlloyDB 的方式。
gcloud
- 建立 Google Cloud 專案或使用現有專案: - gcloud projects create PROJECT_ID --set-as-default # For an existing project gcloud config set project PROJECT_ID- 更改下列內容: - PROJECT_ID:專案的永久 ID。
 
- 請確認您已為 Google Cloud 專案啟用計費功能。瞭解如何確認專案已啟用計費功能。 
- 啟用 AlloyDB、Compute Engine 和 Resource Manager API: - gcloud services enable alloydb.googleapis.com gcloud services enable compute.googleapis.com gcloud services enable cloudresourcemanager.googleapis.com
- 如果您打算使用與 AlloyDB 位於同一個 Google Cloud 專案的虛擬私有雲網路,設定與 AlloyDB 的網路連線,請在專案中啟用 Service Networking API。 - gcloud services enable servicenetworking.googleapis.com
- 如果您打算使用位於不同專案的虛擬私有雲網路,設定與 AlloyDB 的網路連線,請確認該專案已啟用 Compute Engine 和 Service Networking API,以便在該專案中設定共用虛擬私有雲和私人服務存取權。 Google Cloud - gcloud services list
建立叢集及其主要執行個體
- 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。 
- 按一下 [Create cluster] (建立叢集)。 
- 在「設定叢集」中,按照下列步驟操作: - 在「Cluster ID」(叢集 ID) 欄位中輸入 - my-cluster。
- 輸入密碼。請記下這組密碼,因為您會在本次快速入門中使用。 
- 保留預設資料庫版本。 
- 在「Location」(位置)的「Region」(區域) 下拉式選單中,選取「 - us-central1 (Iowa)」區域。
 
- 在「設定主要執行個體」中,按照下列步驟操作: - 在「Instance ID」(執行個體 ID) 欄位中輸入 - my-cluster-primary。
- 在「可用區可用性」中,按一下「單一可用區」。 
- 在「Machine type」(機器類型) 下拉式選單中,選取「 - 2 vCPU, 16 GB」機器類型。
- 在「連線」的「網路」下拉式選單中,選取「 - default」網路。- 如果您有私人存取連線,請繼續執行建立叢集的步驟。如果沒有,請按一下「設定連線」,然後按照下列步驟操作: - 在「Allocate an IP range」(分配 IP 範圍) 中,按一下「Use automatically assigned IP range」(使用系統自動指派的 IP 範圍)。 
- 依序點選「繼續」和「建立連線」。 
 
- 保留「網路安全性」下的預設設定。 
 
- 按一下「Create cluster」(建立叢集)。AlloyDB 可能需要幾分鐘才能建立叢集,並顯示在主要叢集的「總覽」頁面上。 
連線至執行個體並建立資料庫
- 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。 
- 在「資源名稱」資料欄中,按一下叢集名稱 - my-cluster。
- 在導覽窗格中,按一下「AlloyDB Studio」。 
- 在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面,按照下列步驟操作: - 選擇資料庫驗證方法。 
- 選取 - postgres資料庫。
- 按一下「驗證」。「Explorer」窗格會顯示 - postgres資料庫中的物件清單。
 
- 如要建立資料庫,請在「Editor 1」(編輯器 1) 分頁標籤中輸入下列陳述式,然後點選「Run」(執行): - CREATE DATABASE guestbook;- 如果「結果」窗格中顯示 - Statement executed successfully訊息,表示資料庫已建立。
連線至 guestbook 資料庫
- 前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。 
- 在「資源名稱」資料欄中,按一下叢集名稱 - my-cluster。
- 在導覽窗格中,按一下「AlloyDB Studio」。 
- 在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面,按照下列步驟操作: - 選取 - guestbook資料庫。
- 選取 - postgres使用者。
- 輸入您在「建立叢集及其主要執行個體」中建立的密碼。 
- 按一下「驗證」。「Explorer」窗格會顯示 - guestbook資料庫中的物件清單。
 
確認資料庫連線
- 按照「連線至 - guestbook資料庫」一文中的步驟操作。
- 如要將範例資料插入 - guestbook資料庫,請在「Editor 1」(編輯器 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!');
- 按一下「執行」。「結果」窗格中的 - Statement executed successfully訊息表示資料已插入資料庫。
- 如要從「編輯器 1」分頁標籤移除文字,以便執行其他指令,請按一下「清除」。 
- 如要選取資料庫中的所有項目,請在「Editor 1」(編輯器 1) 分頁中輸入下列查詢: - SELECT * FROM entries;
- 按一下「執行」。「Results」(結果) 窗格會顯示類似下列內容的輸出結果: - guestname | content | entryid --------------+-------------+--------- Alex | I got here! | 1 Kai | Me too! | 2
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。