创建并连接到数据库
本页面将引导您完成在 AlloyDB for PostgreSQL 上创建并连接到您的第一个数据库的流程。
本快速入门指南末尾提供了引导您清理这一新数据库的说明。本页面最后提供了后续步骤,可用于详细了解如何将 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. 
- 
 启用创建和连接到 AlloyDB for PostgreSQL 所需的 Cloud API。 - 在确认项目步骤中,点击下一步以确认您要更改的项目的名称。 
- 在启用 API 步骤中,点击启用以启用以下内容: - AlloyDB API
- Compute Engine API
- Cloud Resource Manager API
- Service Networking API
 - 如果您计划使用与 AlloyDB 位于同一 Google Cloud 项目中的 VPC 网络配置与 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:项目的永久性标识符。
 
- 确保您的 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 项目中的 VPC 网络配置与 AlloyDB 的网络连接,请在项目中启用 Service Networking API。 - gcloud services enable servicenetworking.googleapis.com
- 如果您计划使用位于其他 Google Cloud 项目中的 VPC 网络配置与 AlloyDB 的网络连接,请确认该项目中的 Compute Engine 和 Service Networking API 已启用,以便您可以在该项目中配置共享 VPC 和专用服务访问通道。 - gcloud services list
创建集群及其主实例
- 在 Google Cloud 控制台中,前往集群页面。 
- 点击创建集群。 
- 在配置集群中,按以下步骤操作: - 在集群 ID 字段中,输入 - my-cluster。
- 输入密码。请记下此密码,因为您将在本快速入门中使用它。 
- 保留默认数据库版本。 
- 在位置的区域下拉列表中,选择 - us-central1 (Iowa)区域。
 
- 在配置主实例中,按以下步骤操作: - 在实例 ID 字段中,输入 - my-cluster-primary。
- 在可用区级可用性中,点击单个可用区。 
- 在机器类型下拉菜单中,选择 - 2 vCPU, 16 GB机器类型。
- 在连接的网络下拉菜单中,选择 - default网络。- 如果您具有专用访问连接,请继续执行创建集群的步骤。否则,请点击设置连接,然后按照以下步骤操作: - 在分配 IP 范围中,点击使用自动分配的 IP 范围。 
- 点击继续,然后点击创建连接。 
 
- 保留网络安全下的默认设置。 
 
- 点击创建集群。AlloyDB 可能需要几分钟的时间来创建集群并将其显示在主集群概览页面上。 
连接到您的实例并创建数据库
- 在 Google Cloud 控制台中,前往集群页面。 
- 在资源名称列中点击您的集群的名称 - my-cluster。
- 在导航窗格中,点击 AlloyDB Studio。 
- 在登录 AlloyDB Studio 页面中,按以下步骤操作: - 选择数据库身份验证方法。 
- 选择 - postgres数据库。
- 点击身份验证。 探索器窗格会显示 - postgres数据库中的对象列表。
 
- 如需创建数据库,请在编辑器 1 标签页中输入以下语句,然后点击运行: - CREATE DATABASE guestbook;- 当结果窗格中显示 - Statement executed successfully消息时,即表示您的数据库已创建。
连接到 guestbook 数据库
- 在 Google Cloud 控制台中,前往集群页面。 
- 在资源名称列中点击您的集群的名称 - my-cluster。
- 在导航窗格中,点击 AlloyDB Studio。 
- 在登录 AlloyDB Studio 页面中,按以下步骤操作: - 选择 - guestbook数据库。
- 选择 - postgres用户。
- 输入您在创建集群及其主实例中创建的密码。 
- 点击身份验证。 探索器窗格会显示 - guestbook数据库中的对象列表。
 
验证数据库连接
- 按照连接到 - guestbook数据库中的步骤操作。
- 如需将示例数据插入到 - guestbook数据库中,请在编辑器 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 标签页中移除文本,以便运行其他命令,请点击清除。 
- 如需选择数据库中的所有条目,请在编辑器 1 标签页中输入以下查询: - SELECT * FROM entries;
- 点击运行。结果窗格中会显示类似于以下内容的输出: - guestname | content | entryid --------------+-------------+--------- Alex | I got here! | 1 Kai | Me too! | 2
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。