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
- 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 AlloyDB, Compute Engine, Resource Manager, and Service Networking APIs.
Create a cluster and its primary instance
In the Google Cloud console, go to the Clusters page.
Click Create cluster.
Select Highly available 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, select
us-central1 (Iowa)
.Under Networking, select
default
.A dialog labeled Private service access connection required appears.
In the dialog, 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
.Leave the Flags field blank, and click Create cluster.
Create a VM inside your project's VPC
In the Google Cloud console, go to the VM instances page.
Click Create instance.
Name the new VM instance
instance-1
.Set the Region to
us-central1
.Set the Zone to
us-central1-a
.Scroll down to the Identity and API access section.
Set Access scopes to
Allow full access to all Cloud APIs
.Scroll down to Create, and click it.
Wait for Google Cloud to finish creating the VM.
Install psql
on the VM
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.
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
In the Google Cloud console, go to the Clusters page.
Find the row in the table whose Resource name is
my-primary
.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
Return to the window running your SSH session. If you closed it, repeat the first step of Install
psql
on the VM to reconnect.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.
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 apostgres=>
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=>
In
psql
, create a database, and then connect to it:CREATE DATABASE guestbook; \c guestbook
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!');
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
Go to the Clusters page.
Click
my-cluster
.The Overview page for that cluster opens.
Click Delete cluster.
In the Delete cluster dialog that appears, type
my-cluster
again, confirming that you want to delete it.Click Delete.
Clean up the VM instance
Go to the VM instances page.
Click
instance-1
.The
instance-1
overview page opens.Click
Delete
. You might need to click More actions to reveal this button, depending upon the width of your window.In the Delete
instance-1
? dialog that appears, clickDelete
.