Create and query an AlloyDB database using the Google Cloud console

In AlloyDB for PostgreSQL, your databases are stored in clusters that contain instances. In this quickstart, you set up a cluster, connect to its instance, and insert and query sample data. For more information about AlloyDB architecture, clusters, and instances, see AlloyDB overview.

Before you begin

  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.

    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. The Service Networking API is required if you plan to configure network connectivity to AlloyDB using a VPC network that resides in the same Google Cloud project as AlloyDB.

    The Compute Engine API and Cloud Resource Manager API are required if you plan to configure network connectivity to AlloyDB using a VPC network that resides in a different Google Cloud project.

  11. Choose how to connect to AlloyDB.

Required roles

To get the permissions that you need to create and connect to an AlloyDB database, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

These predefined roles contain the permissions required to create and connect to an AlloyDB database. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to create and connect to an AlloyDB database:

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

You might also be able to get these permissions with custom roles or other predefined roles.

Create a cluster and its primary AlloyDB instance

  1. In the Google Cloud console, go to the Clusters page.

    Go to Clusters

  2. Click Create cluster.

  3. In Configure your cluster, follow these steps:

    1. In the Cluster ID field, enter my-cluster.

    2. Enter a password. Take note of this password because you use it in this quickstart.

    3. Retain the default database version.

    4. In the Region drop-down of Location, select the us-central1 (Iowa) region.

  4. In Configure your primary instance, follow these steps:

    1. In the Instance ID field, enter my-cluster-primary.

    2. In Zonal availability, click Single zone.

    3. In the Machine type drop-down, select the 2 vCPU, 16 GB machine type.

    4. In the Network drop-down of Connectivity, select the default network.

      If you have a private access connection, continue to the creating a cluster step. Otherwise, click Set up connection and follow these steps:

      1. In Allocate an IP range, click Use automatically assigned IP range.

      2. Click Continue and then click Create connection.

    5. Retain the default settings under Network security.

  5. Click Create cluster. It might take several minutes for AlloyDB to create the cluster and display it on the primary cluster Overview page.

Connect to your instance and create a database in AlloyDB Studio

  1. In the Google Cloud console, go to the Clusters page.

    Go to Clusters

  2. Click the name of your cluster, my-cluster, in the Resource name column.

  3. In the navigation pane, click AlloyDB Studio.

  4. In the Sign in to AlloyDB Studio page, follow these steps:

    1. Choose a database authentication method.

    2. Select the postgres database.

    3. Click Authenticate. The Explorer pane displays a list of the objects in the postgres database.

  5. To create a database, enter the following statement in the Editor 1 tab and then click Run:

    CREATE DATABASE guestbook;
    

    You know your database is created when the Statement executed successfully message displays in the Results pane.

Connect to the guestbook database

  1. In the Google Cloud console, go to the Clusters page.

    Go to Clusters

  2. Click the name of your cluster, my-cluster, in the Resource name column.

  3. In the navigation pane, click AlloyDB Studio.

  4. In the Sign in to AlloyDB Studio page, follow these steps:

    1. Select the guestbook database.

    2. Select the postgres user.

    3. Enter the password you created in Create a cluster and its primary instance.

    4. Click Authenticate. The Explorer pane displays a list of the objects in the guestbook database.

Verify your database connection

  1. Follow the steps in Connect to the guestbook database.

  2. To insert sample data into the guestbook database, enter the following in the Editor 1 tab:

    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. Click Run. The Statement executed successfully message in the Results pane indicates that data is inserted into your database.

  4. To remove text from the Editor 1 tab so that you can run another command, click Clear.

  5. To select all entries in the database, enter the following query in the Editor 1 tab:

    SELECT * FROM entries;
    
  6. Click Run. Output that is similar to following appears in the Results pane:

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

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. In the Google Cloud console, go to the Clusters page.

    Go to Clusters

  2. Click the name of your cluster, my-cluster, in the Resource name column.

  3. Click Delete cluster.

  4. In Delete cluster my-cluster, enter my-cluster to confirm you want to delete your cluster.

  5. Click Delete.

  6. If you created a private connection when you created a cluster, go to the Google Cloud console Networking page and click Delete VPC network.

What's next