Create and query an AlloyDB database using the Google Cloud console
In AlloyDB for PostgreSQL, your databases are stored in clusters that contain instances . In this quickstart, you set up a cluster, connect to its instance, and insert and query sample data. For more information about AlloyDB architecture, clusters, and instances, see AlloyDB overview .
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide . If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles . -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide . If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Compute Engine, AlloyDB, Cloud Resource Manager, and Service Networking APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles . - Choose how to connect to AlloyDB .
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.
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
(
roles/compute.networkAdmin) - AlloyDB Admin
(
roles/alloydb.admin)
For more information about granting roles, see Manage access to projects, folders, and organizations .
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 permissionssection:
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 or other predefined roles .
Create a cluster and its primary AlloyDB instance
-
In the Google Cloud console, go to the Clusterspage.
-
Click Create cluster.
-
In Configure your cluster, follow these steps:
-
In the Cluster IDfield, enter
my-cluster. -
Enter a password. Take note of this password because you use it in this quickstart.
-
Retain the default database version.
-
In the Regiondrop-down of Location, select the
us-central1 (Iowa)region.
-
-
In Configure your primary instance, follow these steps:
-
In the Instance IDfield, enter
my-cluster-primary. -
In Zonal availability, click Single zone.
-
In the Machine typedrop-down, select the
2 vCPU, 16 GBmachine type. -
In the Networkdrop-down of Connectivity, select the
defaultnetwork.If you have a private access connection, continue to the creating a cluster step. Otherwise, click Set up connectionand follow these steps:
-
In Allocate an IP range, click Use automatically assigned IP range.
-
Click Continueand then click Create connection.
-
-
Retain the default settings under Network security.
-
-
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 in AlloyDB Studio
-
In the Google Cloud console, go to the Clusterspage.
-
Click the name of your cluster,
my-cluster, in the Resource namecolumn. -
In the navigation pane, click AlloyDB Studio.
-
In the Sign in to AlloyDB Studiopage, follow these steps:
-
Choose a database authentication method .
-
Select the
postgresdatabase. -
Click Authenticate. The Explorerpane displays a list of the objects in the
postgresdatabase.
-
-
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 successfullymessage displays in the Resultspane.
Connect to the guestbook
database
-
In the Google Cloud console, go to the Clusterspage.
-
Click the name of your cluster,
my-cluster, in the Resource namecolumn. -
In the navigation pane, click AlloyDB Studio.
-
In the Sign in to AlloyDB Studiopage, follow these steps:
-
Select the
guestbookdatabase. -
Select the
postgresuser. -
Enter the password you created in Create a cluster and its primary instance .
-
Click Authenticate. The Explorerpane displays a list of the objects in the
guestbookdatabase.
-
Verify your database connection
-
Follow the steps in Connect to the
guestbookdatabase . -
To insert sample data into the
guestbookdatabase, 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!' ); -
Click Run. The
Statement executed successfullymessage in the Resultspane indicates that data is inserted into your database. -
To remove text from the Editor 1tab so that you can run another command, click Clear.
-
To select all entries in the database, enter the following query in the Editor 1tab:
SELECT * FROM entries ; -
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.
-
In the Google Cloud console, go to the Clusterspage.
-
Click the name of your cluster,
my-cluster, in the Resource namecolumn. -
Click delete Delete cluster.
-
In Delete cluster my-cluster, enter
my-clusterto confirm you want to delete your cluster. -
Click Delete.
-
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
-
Learn about other ways to connect to AlloyDB .

