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 AlloyDB, Compute Engine, Resource Manager, and Service Networking APIs.

    Enable the APIs

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 Highly available 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, select us-central1 (Iowa).

  7. Under Networking, select default.

    A dialog labeled Private service access connection required appears.

  8. In the dialog, click Set up connection.

    A new pane labeled Enable Service Networking API appears.

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

  10. Click Continue.

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

    The Enable Service Networking API pane is removed.

  12. Click Continue.

    The Configure your primary instance section expands.

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

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

  15. Leave the Flags field blank, and click Create cluster.

Create a VM inside your project's VPC

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Click Create instance.

  3. Name the new VM instance instance-1.

  4. Set the Region to us-central1.

  5. Set the Zone to us-central1-a.

  6. Scroll down to the Identity and API access section.

  7. Set Access scopes to Allow full access to all Cloud APIs.

  8. Scroll down to Create, and click it.

  9. Wait for Google Cloud to finish creating the VM.

Install psql on the VM

  1. In the list of virtual machine instances, click SSH in the row representing the instance- VM instance.

    A new terminal window appears. Wait for key transfer and login to complete.

  2. Run the following commands in the terminal to install the psql client from the package manager:

    sudo apt-get update
    sudo apt-get install --yes postgresql-client
    

    Leave this terminal window open. You use it again later in this quickstart.

Find the IP address for your new instance

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

    Go to Clusters

  2. Find the row in the table whose Resource name is my-primary.

  3. Take note of that row's Private IP address. You need it for the next step of this quickstart.

Connect to your instance and create a database

  1. Return to the window running your SSH session. If you closed it, repeat the first step of Install psql on the VM to reconnect.

  2. Connect to your instance with the psql client tool:

    psql -h IP_ADDRESS -U postgres
    

    Replace IP_ADDRESS with the instance's IP address found in the previous section.

  3. When prompted, enter the password that you chose for the postgres user earlier in this quickstart.

    The terminal window displays some psql login text, ending with a postgres=> prompt:

    psql (13.11 (Debian 13.11-0+deb11u1), server 14.5)
    SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
    Type "help" for help.
    
    postgres=>
    
  4. In psql, create a database, and then connect to it:

    CREATE DATABASE guestbook;
    \c guestbook
    
  5. Insert sample data into the database:

    CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255),
                            entryID SERIAL PRIMARY KEY);
    INSERT INTO entries (guestName, content) values ('first guest', 'I got here!');
    INSERT INTO entries (guestName, content) values ('second guest', 'Me too!');
    
  6. Retrieve the data:

    SELECT * FROM entries;
    

    The result is:

          guestname   |   content   | entryid
        --------------+-------------+---------
         first guest  | I got here! |       1
         second guest | Me too!     |       2
        (2 rows)
    

From here, you could continue entering SQL commands to further define and populate the database, or use the instance's IP address to connect to it from an application and programmatically set up the new database.

Clean up

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

Clean up the cluster

  1. Go to the Clusters page.

    Go to Clusters

  2. Click my-cluster.

    The Overview page for that cluster opens.

  3. Click Delete cluster.

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

  5. Click Delete.

Clean up the VM instance

  1. Go to the VM instances page.

    Go to VM instances

  2. Click instance-1.

    The instance-1 overview page opens.

  3. Click Delete. You might need to click More actions to reveal this button, depending upon the width of your window.

  4. In the Delete instance-1? dialog that appears, click Delete.

What's next