使用 Google Cloud 控制台建立及查詢 AlloyDB 資料庫
如要在 Google Cloud 控制台開始使用 AlloyDB for PostgreSQL,您可以建立叢集和主要執行個體、使用 AlloyDB Studio 連線至執行個體,以及插入和查詢範例資料。在 AlloyDB 中,資料庫會儲存在包含執行個體的叢集中。如要進一步瞭解 AlloyDB 架構、叢集和執行個體,請參閱「AlloyDB 總覽」。
事前準備
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
如要使用與 AlloyDB 位於相同 Google Cloud 專案的虛擬私有雲 (VPC) 網路,設定 AlloyDB 的網路連線,就必須啟用 Service Networking API。
如果您打算使用位於其他 Google Cloud 專案的虛擬私有雲網路,設定 AlloyDB 的網路連線,就必須啟用 Compute Engine API 和 Cloud Resource Manager API。
- 選擇連線選項。
必要的角色
如要取得建立及連線至 AlloyDB 資料庫所需的權限,請要求管理員授予您專案的下列 IAM 角色:
- Compute 網路管理員 (
roles/compute.networkAdmin) - AlloyDB 管理員 (
roles/alloydb.admin)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這些預先定義的角色具備建立及連線至 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 執行個體
前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。
點選「建立叢集」。
在「設定叢集」中,按照下列步驟操作:
在「Cluster ID」(叢集 ID) 欄位中輸入
my-cluster。輸入密碼。請記下這個密碼,因為您會在本快速入門導覽課程中使用。
保留預設資料庫版本。
在「位置」下方的「區域」選單中,選取「
us-central1 (Iowa)」區域。
在「設定主要執行個體」部分,按照下列步驟操作:
在「Instance ID」(執行個體 ID) 欄位中輸入
my-cluster-primary。在「可用區可用性」部分,選取「單一可用區」。
在「Machine type」(機器類型) 選單中,選取「
2 vCPU, 16 GB」機器類型。在「Connectivity」(連線) 下方的「Network」(網路) 選單中,選取
default網路。如果您已設定私人服務存取連線,請繼續下一個步驟。如果沒有,請按一下「設定連線」,然後按照下列步驟操作:
在「Allocate an IP range」(分配 IP 範圍) 中,選取「Use an automatically allocated IP range」(使用系統自動分配的 IP 範圍)。
依序點選「繼續」和「建立連線」。
保留「網路安全」下方的預設設定。
按一下「Create cluster」(建立叢集)。AlloyDB 可能需要幾分鐘才能建立叢集,並顯示在主要叢集的「Overview」(總覽) 頁面。
在 AlloyDB Studio 連線至執行個體並建立資料庫
前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。
在「Resource name」(資源名稱) 欄中,按一下叢集名稱
my-cluster。在導覽窗格中,點選「AlloyDB Studio」。
在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面中,按照下列步驟操作:
選擇資料庫驗證方式。
選取
postgres資料庫。按一下「Authenticate」(驗證)。「Explorer」窗格會顯示
postgres資料庫中的物件清單。
如要建立資料庫,請在「Editor 1」(編輯器 1) 分頁中輸入下列陳述式,然後按一下「Run」(執行):
CREATE DATABASE guestbook;「Results」(結果) 窗格顯示
Statement executed successfully訊息時,就表示資料庫已建立。
連線到 guestbook 資料庫
前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。
在「Resource name」(資源名稱) 欄中,按一下叢集名稱
my-cluster。在導覽窗格中,點選「AlloyDB Studio」。
在「Sign in to AlloyDB Studio」(登入 AlloyDB Studio) 頁面中,按照下列步驟操作:
選取
guestbook資料庫。選取
postgres使用者。輸入您在「建立叢集及其主要 AlloyDB 執行個體」中建立的密碼。
按一下「Authenticate」(驗證)。「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!');按一下「執行」。「Results」窗格中的
Statement executed successfully訊息表示資料已插入資料庫。如要移除「Editor 1」分頁中的文字,以便執行其他指令,請按一下「Clear」(清除)。
如要選取資料庫中的所有項目,請在「Editor 1」(編輯器 1) 分頁中輸入下列查詢:
SELECT * FROM entries;按一下「執行」。「Results」(結果) 窗格中會顯示類似以下的輸出內容:
guestname | content | entryid --------------+-------------+--------- Alex | I got here! | 1 Kai | Me too! | 2
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
前往 Google Cloud 控制台的「Clusters」(叢集) 頁面。
在「Resource name」(資源名稱) 欄中,按一下叢集名稱
my-cluster。按一下「delete 刪除叢集」。
在「Delete cluster my-cluster」(刪除叢集 my-cluster) 中,輸入
my-cluster,確認要刪除叢集。點選「刪除」。
選用:如果您在建立叢集時建立了私人服務存取連線,且不再需要分配的 IP 位址範圍,請按照「移除私人服務存取權分配範圍」中的步驟操作。