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
- 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.
-
Verify that billing is enabled for your Google Cloud project .
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Cloud APIs necessary to create and connect to AlloyDB for PostgreSQL.
-
In the Confirm project step, click Next to confirm the name of the project you are going to make changes to.
-
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
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project. Learn how to confirm that billing is enabled for your project .
-
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
-
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
-
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
-
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 GB
machine type. -
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:
-
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 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
postgres
database. -
Click Authenticate. The Explorerpane displays a list of the objects in the
postgres
database.
-
-
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
-
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
guestbook
database. -
Select the
postgres
user. -
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
guestbook
database.
-
Verify your database connection
-
Follow the steps in Connect to the
guestbook
database . -
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!' );
-
Click Run. The
Statement executed successfully
message 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-cluster
to 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.