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

Create a cluster and its primary instance

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

    Go to Clusters

  2. Click Create cluster.

    Select Production Workloads from the list of available cluster types.

  3. Click Continue.

    The Configure your cluster section expands.

  4. In the Cluster ID field of the Basic info section, enter my-cluster.

  5. In the Password field, enter any password you'd like. Take note of this password—you use it again later in this quickstart.

  6. In the Region field of the Location section, select us-central1 (Iowa).

  7. In the Database version field, keep the default value.

  8. Under Networking, select default.

    If you have not set up private services access yet, a dialog labeled Private service access connection required appears.

    In the Private service access connection required dialog, to create private services access connection, complete the following:

    1. Click Set up connection.

      A new pane labeled Enable Service Networking API appears.

    2. In the pane, select Use an automatically allocated IP range.

    3. Click Continue.

    4. Click Create connection, and wait for the connection configuration to finish.

      The Enable Service Networking API pane is removed.

  9. Click Continue.

    The Configure your primary instance section expands.

  10. Under Basic info, in the Instance ID field, enter my-primary.

  11. For a Machine value, select 2 vCPU, 16 GB.

  12. Click Create cluster.

    Wait for the cluster to get created and the cluster Overview page to display.

Connect to your instance and create a database

  1. In the navigation menu of the cluster Overview page, click AlloyDB Studio.

  2. In the Sign in to AlloyDB Studio page, select postgres in the Database list.

  3. Select postgres in the User list.

  4. In the Password field, enter the password you created in Create a cluster and its primary instance.

  5. Click Authenticate. The Explorer pane displays a list of the objects in your database.

  6. In the Editor 1 tab, to create a database, enter the following statement:

    CREATE DATABASE guestbook;
    
  7. Click Run. Wait for the Statement executed successfully message to display in the Results pane.

    Your guestbook database is now created.

Connect to the guestbook database

  1. In the Explorer pane of the AlloyDB Studio, click Switch user/database.

  2. In the Sign in to AlloyDB Studio page, select guestbook in the Database list.

  3. Select postgres in the User list.

  4. In the Password field, enter the password you created in Create a cluster and its primary instance.

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

Verify your database connection

  1. In the Editor 1 tab, insert sample data into the guestbook database:

    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!');
    
  2. Click Run. Wait for the Statement executed successfully message to display in the Results pane.

  3. To remove the sample data from the tab, click Clear.

  4. In the Editor 1 tab, paste the following query:

    SELECT * FROM entries;
    
  5. To execute your query, click Run.

    In the Results pane, the result that appears looks similar to the following:

          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 navigation menu, click Overview.

  2. Click Delete cluster.

  3. In the Delete cluster dialog that appears, type my-cluster again, confirming that you want to delete it.

  4. Click Delete.

What's next