This page shows you how to create and connect to a Cloud SQL PostgreSQL instance and perform basic SQL operations using the Google Cloud Platform Console and the psql client.
Before you begin
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
Select or create a GCP project.
Make sure that billing is enabled for your project.
...see naming guidelines
- Enable the Cloud SQL Administration API.
Create a Cloud SQL instance
Go to the Cloud SQL Instances page in the Google Cloud Platform Console.
Select your project and click Continue.
- Click Create Instance.
- Click PostgreSQL.
myinstancefor Instance ID.
Enter a password for the postgres user.
Use the default values for the other fields.
You are returned to the instances list; your new instance is greyed out while it initializes and starts.
Connect to your instance using the psql client in Cloud Shell
In the Google Cloud Platform Console, click the Cloud Shell icon () in the upper right corner.
When Cloud Shell finishes initializing, you should see:
Welcome to Cloud Shell! Type "help" to get started. username@example-id:~$
At the Cloud Shell prompt, connect to your Cloud SQL instance:
gcloud sql connect myinstance --user=postgres
Enter your postgres password.
You should see the psql prompt.
Create a database and upload data
- Insert sample data into the postgres database:
CREATE TABLE guestbook (guestName VARCHAR(255), content VARCHAR(255), entryID SERIAL PRIMARY KEY); INSERT INTO guestbook (guestName, content) values ('first guest', 'I got here!'); INSERT INTO guestbook (guestName, content) values ('second guest', 'Me too!');
- Retrieve the data:
SELECT * FROM guestbook;You should see:
guestname | content | entryid --------------+-------------+--------- first guest | I got here! | 1 second guest | Me too! | 2 (2 rows) postgres=>
To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:
- Go to the Cloud SQL Instances page in the Google Cloud Platform Console.
Go to the Cloud SQL Instances page
- Select the
myinstanceinstance to open the Instance details page.
- In the icon bar at the top of the page, click Delete.
In the Delete instance window, type
myinstance, then click Delete to delete the instance.
You cannot reuse an instance name for approximately 7 days after an instance is deleted.
- Learn more about creating Cloud SQL instances.
- Learn about connecting to a Cloud SQL instance:
- Learn about creating PostgreSQL users and databases for your Cloud SQL instance.