This page describes how to connect the psql
client tool to
an AlloyDB instance.
The procedure to connect psql
to an AlloyDB
instance involves these tasks:
- Create a Compute Engine VM that can connect to AlloyDB instances using private services access.
- Install the
psql
client on the Compute Engine VM. - Get the IP address of the AlloyDB instance you want to connect to.
- Run the
psql
client.
The first two of these tasks are one-time operations. After you create a
Compute Engine VM and install the psql
client on it, you can continue
to use the VM to connect psql
to AlloyDB instances by
performing remaining tasks.
Before you begin
- The Google Cloud project you are using must have been enabled to access AlloyDB.
- A VPC network in the Google Cloud project that you are using must already be configured for private services access to AlloyDB.
- You must have one of these IAM roles in the Google Cloud project you are using:
roles/alloydb.admin
(the AlloyDB Admin predefined IAM role)roles/owner
(the Owner basic IAM role)roles/editor
(the Editor basic IAM role)
If you don't have any of these roles, contact your Organization Administrator to request access.
- If the client host has a firewall, it must allow egress connections to port 5432 on your AlloyDB instances' IP addresses.
Create a Compute Engine VM
In the Google Cloud console, go to the VM instances page.
- Select the project containing the AlloyDB instance you want to connect to.
- Click Create instance.
- Click the Machine configuration section.
- Enter a Name for the instance.
- Configure the region and zone where you want to create the instance.
- Select a machine series and the corresponding machine type.
- Retain the default values in the OS and Storage section.
- Click the Networking section, and set Network interfaces to the VPC network configured for private services access to AlloyDB.
If Network interfaces is not set to the VPC network configured for private services access, expand it and then set Network to the VPC network. - Retain the default values in the Observability section.
- Click the Security section.
- In Identity and API access, set Access scopes to Allow full access to all Cloud APIs.
- Retain the default values in the Advanced section.
- Click Create.
After the VM is created, connect to the Compute Engine VM you created using SSH.
Install the psql client tool
- Connect to the Compute Engine VM you created using SSH.
Console
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance you created.
gcloud
Use the
gcloud compute ssh
command to connect to the instance you created.gcloud compute ssh --project=PROJECT_ID --zone=ZONE VM_NAME
Replace the following:
PROJECT_ID
: the ID of the project that contains the instanceZONE
: the name of the zone in which the instance is locatedVM_NAME
: the name of the instance
- Install the psql client from the package manager:
sudo apt-get update sudo apt-get install postgresql-client
Get the IP address of the AlloyDB instance
To get the IP address of the AlloyDB instance you want to connect to, view that instance's settings.
Run the psql client
- Connect to the VM you created using SSH.
Console
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance you created.
gcloud
Use the
gcloud compute ssh
command to connect to the instance you created.gcloud compute ssh --project=PROJECT_ID --zone=ZONE VM_NAME
Replace the following:
PROJECT_ID
: the ID of the project that contains the instanceZONE
: the name of the zone in which the instance is locatedVM_NAME
: the name of the instance
- Run the
psql
client tool:psql -h IP_ADDRESS -U USERNAME
You will be prompted to enter the password of the USERNAME user.
If you're connecting for the first time, use the default value
postgres
for USERNAME and the password you used while creating the cluster for PASSWORD.