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

Console

  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. Verify 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. Verify that billing is enabled for your Google Cloud project .

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

gcloud

  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

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

  2. Click Create cluster.

  3. In Configure your cluster, follow these steps:

    1. In the Cluster IDfield, 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 Regiondrop-down of Location, select the us-central1 (Iowa) region.

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

    1. In the Instance IDfield, enter my-cluster-primary .

    2. In Zonal availability, click Single zone.

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

    4. In the Networkdrop-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 connectionand follow these steps:

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

      2. Click Continueand 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 Overviewpage.

Connect to your instance and create a database

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

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

  3. In the navigation pane, click AlloyDB Studio.

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

    1. Choose a database authentication method .

    2. Select the postgres database.

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

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

      CREATE 
      
     DATABASE 
      
     guestbook 
     ; 
     
    

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

Connect to the guestbook database

  1. In the Google Cloud console, go to the Clusterspage.

    Go to Clusters

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

  3. In the navigation pane, click AlloyDB Studio.

  4. In the Sign in to AlloyDB Studiopage, 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 Explorerpane 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 1tab:

      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 Resultspane indicates that data is inserted into your database.

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

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

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

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

    Go to Clusters

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

  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

Design a Mobile Site
View Site in Mobile | Classic
Share by: