# Create and query an AlloyDB database using the Google Cloud console

To get started with AlloyDB for PostgreSQL in the Google Cloud console, you can create a
cluster and primary instance, connect to the instance by using
AlloyDB Studio, and insert and query sample data. In AlloyDB,
your databases are stored in *clusters* that contain *instances* . For more
information about AlloyDB architecture, clusters, and instances,
see [AlloyDB overview](https://docs.cloud.google.com/alloydb/docs/overview.md.txt).

## Before you begin


1. The Service Networking API is required if you plan to configure
   network connectivity to AlloyDB using a Virtual Private Cloud (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.
2. [Choose a connectivity option](https://docs.cloud.google.com/alloydb/docs/choose-alloydb-connectivity.md.txt).

<br />

### 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:

- [Compute Network Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/compute.md.txt#compute.networkAdmin) (`roles/compute.networkAdmin`)
- [AlloyDB Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/alloydb.md.txt#alloydb.admin) (`roles/alloydb.admin`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


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](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

## Create a cluster and its primary AlloyDB instance

> [!NOTE]
> **Note:** To set up a cluster, instance, and network for your development environment using an interactive guide, follow the steps in the [AlloyDB setup codelab](https://codelabs.developers.google.com/easy-alloydb-setup).

1. In the Google Cloud console, go to the **Clusters** page, or go to the **Get
   started** page.

   [Go to Clusters](https://console.cloud.google.com/alloydb/clusters)
2. If your Google Cloud project already has clusters, click **Create
   cluster** to open the **Select configuration** page.

3. In the **Production (single zone)** card, click **Create cluster**.

4. In the cluster creation pane that opens, click **Customize** to open the
   full cluster creation form.

5. 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** menu under **Location** , select the `us-central1 (Iowa)` region.

6. In **Configure your primary instance**, follow these steps:

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

   2. In **Zonal availability** , select **Single zone**.

      > [!NOTE]
      > **Note:** While we don't recommend a single zone instance for production, you can use a single zone in this quickstart to minimize costs. For more information, see [Reduce costs using basic instances](https://docs.cloud.google.com/alloydb/docs/basic-instance.md.txt).

   3. In the **Machine type** menu, select the `2 vCPU, 16 GB` machine type.

   4. In the **Network** menu under **Connectivity** , select the `default` network.

      If you have a private services access connection configured, continue to the next step.
      Otherwise, click **Set up connection** and follow these steps:
      1. In **Allocate an IP range** , select
         **Use an automatically allocated IP range**.

      2. Click **Continue** and then click **Create connection**.

   5. Retain the default settings under **Network security**.

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

> [!NOTE]
> **Note:** To connect a `psql` client to an instance, follow the steps in [Connect from Compute Engine](https://docs.cloud.google.com/alloydb/docs/connect-psql.md.txt).

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

   [Go to Clusters](https://console.cloud.google.com/alloydb/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](https://docs.cloud.google.com/alloydb/docs/connection-overview.md.txt#authn).

   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**:

   ```sql
   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](https://console.cloud.google.com/alloydb/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 AlloyDB instance](https://docs.cloud.google.com/alloydb/docs/quickstart/create-and-connect.md.txt#create-cluster).

   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](https://docs.cloud.google.com/alloydb/docs/quickstart/create-and-connect.md.txt#connect-to-guestbook).

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

   ```sql
   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:

   ```sql
   SELECT * FROM entries;
   ```
6. Click **Run** . Output that is similar to the 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](https://console.cloud.google.com/alloydb/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. Optional: If you created a private services access connection when you
   [created the cluster](https://docs.cloud.google.com/alloydb/docs/quickstart/create-and-connect.md.txt#create-cluster) and no longer need the allocated IP
   address range, follow the steps in
   [Remove a private services access allocated range](https://docs.cloud.google.com/alloydb/docs/psa-remove-allocated-range.md.txt).

## What's next

- Learn about other [ways to connect to AlloyDB](https://docs.cloud.google.com/alloydb/docs/choose-alloydb-connectivity.md.txt).

- [Grant access to other users](https://docs.cloud.google.com/alloydb/docs/user-grant-access.md.txt).

- [Enable private services access](https://docs.cloud.google.com/alloydb/docs/configure-connectivity.md.txt).

- [Choose an AlloyDB machine type](https://docs.cloud.google.com/alloydb/docs/choose-machine-type.md.txt).

- [Create an AlloyDB cluster and its primary instance](https://docs.cloud.google.com/alloydb/docs/cluster-create.md.txt).

- [Learn about sizing recommendations](https://docs.cloud.google.com/alloydb/docs/use-sizing-recommendations.md.txt).

- [Connect from Compute Engine](https://docs.cloud.google.com/alloydb/docs/connect-psql.md.txt).