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.
-
Make sure 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.
-
Make sure 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
-
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.
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 API and Service Networking API in that project are enabled so that you can configure Shared VPC and private services access in that 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 Clusters page.
Click Create cluster.
Select Production Workloads from the list of available cluster types.
Click Continue.
The Configure your cluster section expands.
In the Cluster ID field of the Basic info section, enter
my-cluster
.In the Password field, enter any password you'd like. Take note of this password—you use it again later in this quickstart.
In the Region field of the Location section, select
us-central1 (Iowa)
.In the Database version field, keep the default value.
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:
Click Set up connection.
A new pane labeled Enable Service Networking API appears.
In the pane, select Use an automatically allocated IP range.
Click Continue.
Click Create connection, and wait for the connection configuration to finish.
The Enable Service Networking API pane is removed.
Click Continue.
The Configure your primary instance section expands.
Under Basic info, in the Instance ID field, enter
my-primary
.For a Machine value, select
2 vCPU, 16 GB
.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
In the navigation menu of the cluster Overview page, click AlloyDB Studio.
In the Sign in to AlloyDB Studio page, select
postgres
in the Database list.Select
postgres
in the User list.In the Password field, enter the password you created in Create a cluster and its primary instance.
Click Authenticate. The Explorer pane displays a list of the objects in your database.
In the Editor 1 tab, to create a database, enter the following statement:
CREATE DATABASE guestbook;
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
In the Explorer pane of the AlloyDB Studio, click manage_accounts Switch user/database.
In the Sign in to AlloyDB Studio page, select
guestbook
in the Database list.Select
postgres
in the User list.In the Password field, enter the password you created in Create a cluster and its primary instance.
Click Authenticate. The Explorer pane displays a list of the objects in the
guestbook
database.
Verify your database connection
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!');
Click Run. Wait for the
Statement executed successfully
message to display in the Results pane.To remove the sample data from the tab, click Clear.
In the Editor 1 tab, paste the following query:
SELECT * FROM entries;
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.
In the navigation menu, click Overview.
Click delete Delete cluster.
In the Delete cluster dialog that appears, type
my-cluster
again, confirming that you want to delete it.Click Delete.