创建并连接到数据库

本页面将引导您完成在 AlloyDB for PostgreSQL 上创建并连接到您的第一个数据库的流程。

本快速入门指南末尾提供了引导您清理这一新数据库的说明。本页面最后提供了后续步骤,可用于详细了解如何将 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. Make sure 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. Make sure that billing is enabled for your Google Cloud project.

  6. 启用创建和连接到 AlloyDB for PostgreSQL 所需的 Cloud API。

    启用 API

    1. 确认项目步骤中,点击下一步以确认您要更改的项目的名称。

    2. 启用 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。

gcloud

  1. 创建 Google Cloud 项目或使用现有项目:

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

    替换以下内容:

    • PROJECT_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 项目中的 VPC 网络配置与 AlloyDB 的网络连接,请在项目中启用 Service Networking API。

    gcloud services enable servicenetworking.googleapis.com
    
  5. 如果您计划使用位于其他 Google Cloud 项目中的 VPC 网络配置与 AlloyDB 的网络连接,请确认该项目中的 Compute Engine 和 Service Networking API 已启用,以便您可以在该项目中配置共享 VPC 和专用服务访问通道。

    gcloud services list
    

创建集群及其主实例

注意:如果您计划使用专用网络,可以使用 Terraform 部署所选择的专用网络设置和 AlloyDB 实例以及 Compute Engine 虚拟机等客户端。如需了解详情,请参阅简化的 Cloud 网络产品配置解决方案

  1. 在 Google Cloud 控制台中,前往集群页面。

    转到集群

  2. 点击创建集群

  3. 集群 ID 中,输入 my-cluster

  4. 输入密码。请记下此密码,因为您将在本快速入门中使用它。

  5. 保留默认数据库版本。

  6. 选择 us-central1 (Iowa) 区域。

  7. 选择默认网络。

    如果您具有专用访问连接,请继续执行下一步。否则,请点击设置连接,然后按照以下步骤操作:

    1. 分配 IP 范围中,点击使用自动分配的 IP 范围

    2. 点击继续,然后点击创建连接

  8. 保留默认实例 ID my-cluster-primary

  9. 可用区级可用性部分,保留单个可用区作为所选选项。虽然我们不建议在生产环境中使用单个可用区实例,但您可以在本快速入门中使用单个可用区以尽可能降低成本。如需了解详情,请参阅使用基本实例降低成本

  10. 选择 2 vCPU, 16 GB 机器类型。

  11. 保留连接网络安全下的默认设置。

  12. 请勿在显示高级选项中进行更改。

  13. 点击创建集群。AlloyDB 可能需要几分钟的时间来创建集群并将其显示在主集群概览页面上。

连接到您的实例并创建数据库

  1. 在 Google Cloud 控制台中,前往集群页面。

    转到集群

  2. 资源名称列中点击您的集群的名称 my-cluster

  3. 在导航窗格中,点击 AlloyDB Studio

  4. 登录 AlloyDB Studio 页面中,按以下步骤操作:

    1. 选择 postgres 数据库。

    2. 选择 postgres 用户。

    3. 输入您在创建集群及其主实例中创建的密码。

    4. 点击身份验证探索器窗格会显示 postgres 数据库中的对象列表。

  5. 如需创建数据库,请在编辑器 1 标签页中输入以下语句,然后点击运行

    CREATE DATABASE guestbook;
    

    结果窗格中显示 Statement executed successfully 消息时,即表示您的数据库已创建。

连接到 guestbook 数据库

  1. 在 Google Cloud 控制台中,前往集群页面。

    转到集群

  2. 资源名称列中点击您的集群的名称 my-cluster

  3. 在导航窗格中,点击 AlloyDB Studio

  4. 登录 AlloyDB Studio 页面中,按以下步骤操作:

    1. 选择 guestbook 数据库。

    2. 选择 postgres 用户。

    3. 输入您在创建集群及其主实例中创建的密码。

    4. 点击身份验证探索器窗格会显示 guestbook 数据库中的对象列表。

验证数据库连接

  1. 按照连接到 guestbook 数据库中的步骤操作。

  2. 如需将示例数据插入到 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!');
    
  3. 点击运行结果窗格中的 Statement executed successfully 消息表示数据已插入到您的数据库中。

  4. 如需从编辑器 1 标签页中移除文本,以便运行其他命令,请点击清除

  5. 如需选择数据库中的所有条目,请在编辑器 1 标签页中输入以下查询:

    SELECT * FROM entries;
    
  6. 点击运行结果窗格中会显示类似于以下内容的输出:

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

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

  1. 在 Google Cloud 控制台中,前往集群页面。

    转到集群

  2. 资源名称列中点击您的集群的名称 my-cluster

  3. 点击 删除集群

  4. 删除集群 my-cluster 中,输入 my-cluster 以确认您要删除集群。

  5. 点击删除

  6. 如果您在创建集群时创建了专用连接,请前往 Google Cloud 控制台的网络页面,然后点击删除 VPC 网络

后续步骤