Create and connect to a database

This page guides you through the process of creating and connecting to your first database on AlloyDB for PostgreSQL.

Instructions provided at the end of this quickstart guide you through cleaning up this new database. The page concludes with next steps to learn more about using AlloyDB with your own applications.

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.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud APIs necessary to create and connect to AlloyDB for PostgreSQL.

    Enable the APIs

    1. In the Confirm project step, click Next to confirm the name of the project you are going to make changes to.

    2. In the Enable APIs step, click Enable to enable the following:

      • AlloyDB API
      • Compute Engine API
      • Cloud Resource Manager API
      • Service Networking API

      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.

  1. Create a Google Cloud project or use an existing project:

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

    Replace the following:

    • PROJECT_ID: the permanent identifier for the project.
  2. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm that billing is enabled for your project.

  3. Enable the AlloyDB, Compute Engine, and Resource Manager APIs:

    gcloud services enable alloydb.googleapis.com
    gcloud services enable compute.googleapis.com
    gcloud services enable cloudresourcemanager.googleapis.com
    
  4. If you plan to configure network connectivity to AlloyDB using a VPC network that resides in the same Google Cloud project as AlloyDB, then enable the Service Networking API in the project.

    gcloud services enable servicenetworking.googleapis.com
    
  5. If you plan to configure network connectivity to AlloyDB using a VPC network that resides in a different Google Cloud project, then confirm that the Compute Engine and Service Networking API in that project are enabled so that you can configure Shared VPC and private services access in that project.

    gcloud services list
    

Create a cluster and its primary instance

NOTE : If you plan on using private networking, then you can deploy both the private networking setup of your choice and the AlloyDB instance along with clients such as Compute Engine VMs by using Terraform. For more information, see Simplified Cloud Networking Configuration Solutions.

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

    Go to Clusters

  2. Click Create cluster.

  3. In Cluster ID, enter my-cluster.

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

  5. Retain the default database version.

  6. Select the us-central1 (Iowa) region.

  7. Select the default network.

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

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

    2. Click Continue and then click Create connection.

  8. Retain the default instance ID, my-cluster-primary.

  9. In Zonal availability, retain Single zone as the selected option. While we don't recommend a single zone instance for production, you use a single zone in this quickstart to minimize costs. For more information, see Reduce costs using basic instances.

  10. Select the 2 vCPU, 16 GB machine type.

  11. Retain the default settings under Connectivity and Network security.

  12. Don't make changes in Show advanced options.

  13. 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

  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 postgres 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 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