Create and query a Cloud SQL for PostgreSQL database using theGoogle Cloud console
Learn how to create and query a Cloud SQL for PostgreSQL database using theGoogle Cloud console. This quickstart is intended for database practitioners seekinga quick introduction to Cloud SQL for PostgreSQL. The quickstart takes you through thesteps required to complete the following tasks:
- Create a Cloud SQL for PostgreSQL instance
- Create a database
- Create a schema
- Create a table
- Insert data
- Query the data that you inserted
- Clean up your resources
As you follow along, keep the default values for settings unless otherwisespecified.
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.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL, Cloud SQL Admin, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL, Cloud SQL Admin, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.
Required roles
To get the permissions that you need to complete this quickstart, ask your administrator to grant you theCloud SQL Admin (roles/cloudsql.admin) IAM role on the project. For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Create a Cloud SQL for PostgreSQL instance
Create an instance in the Google Cloud console using the following settings.For all other settings, keep the default.
- Go to the Cloud SQL Instances page in the Google Cloud console.
Go to the Cloud SQL Instances page - ClickCreate Instance.
- ClickChoose Cloud SQL for PostgreSQL.
- ForChoose a Cloud SQL edition, chooseEnterprise.
- ForEdition preset, selectSandbox.
- ForInstance ID, enter
quickstart-instance. - Enter a password for the default user account and save it for future use.
- Choose a region near you.
- ForZonal availability, selectSingle zone.
- ClickCreate instance, and then wait until the instance initializes andstarts. The initialization process can take more than five minutes.
Create a database
Afterquickstart-instance has been created, you cancreate a database in your Cloud SQL for PostgreSQL instance.
- In the instance navigation menu, clickDatabases.
- ClickCreate a database.
- In theDatabase Name text field, enter
quickstartdb. - ClickCreate.
Add a user
Before you can read or write to the database, you must create a database userthat is different from the root user.
- In the instance navigation menu, clickUsers.
- ClickAdd user account.
- In the pane that opens, selectBuilt-in authentication.
- In theUser name field, enter
quickstart-user. - Enter a password for the new user. Save this password for future use.
- ClickAdd.
Create a schema
After you've created your instance and database, you can navigate toCloud SQLStudio and use the query editor to create a schema.
- In the instance navigation menu, clickCloud SQL Studio. A dialog isdisplayed.
- In theDatabase drop-down, choosequickstartdb.
- SelectBuilt-in database authentication.
- In theUser drop-down, selectquickstart-user.
- In thePassword field, enter the password that you chose for the user intheAdd a user section.
- ClickAuthenticate. Cloud SQL Studio opens.
- ClickUntitled Query to open the query editor.
Paste the following code into the query editor:
CREATESCHEMAIFNOTEXISTS"myschema";Optional: to correctly format the SQL statement, clickFormat.
ClickRun. The results pane displays a success message.
Create a table
Now create a table using the schema that you created.
- ClickNew tab to open a newquery editor tab.
Paste the following statement into the query editor:
CREATETABLEIFNOTEXISTS"myschema"."quickstart_table"("UserId"SERIALPRIMARYKEY,"FirstName"VARCHAR(255),"LastInitial"VARCHAR(1),"BirthDate"DATE);Optional: to correctly format the SQL statement, clickFormat.
ClickRun. The results pane displays a success message.
Thequickstartdb database now has a table with the columns to store thefollowing data:
- An automatically incrementing user ID column that is configured to be thetable's primary key
- First name
- Last initial
- Birthdate
Insert data
To populate thequickstart_table table with some data, take the following steps:
- ClickNew tab to open a new query editor tab.
Paste the following statement into the query editor:
INSERTINTO"myschema"."quickstart_table"("FirstName","LastInitial","BirthDate")VALUES('Yuri','Z','1999-05-24'),('Cruz','S','1978-11-01'),('Kai','D','1965-12-09'),('Luka','L','2003-04-19'),('Taylor','S','2001-01-31');Optional: to format the SQL statement correctly, clickFormat.
ClickRun. The results pane displays a success message.
Query the database
Now you can run queries against the data in the table that you created.
Select all records
To retrieve all records in the table, do the following:
ClickNew tab to open a newquery editor tab.
Paste the following statement into the query editor:
SELECT*FROM"myschema"."quickstart_table"ClickRun.
The query results are similar to the following:
UserID
FirstName
LastInitial
BirthDate
1
Yuri
Z
1999-05-24 00:00:00
2
Cruz
S
1978-11-01 00:00:00
3
Kai
D
1965-12-09 00:00:00
4
Luka
L
2003-04-19 00:00:00
5
Taylor
S
2001-01-31 00:00:00
Run a filtered select statement
To retrieve the user ID and first name of users born on or after January 1,2000, specify the columns and use aWHERE clause:
- ClickNew tab to open a newquery editor tab.
Paste the following statement into the query editor:
SELECT"UserId","FirstName"FROM"myschema"."quickstart_table"WHERE"BirthDate" >'1999-12-31';ClickRun.
The query results are similar to the following:
UserID
FirstName
4
Luka
5
Taylor
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Disable deletion protection and then delete the quickstart instance:
- In the instance navigation menu, clickOverview.
- ClickEdit.
- Expand theData Protection section.
- InInstance deletion protection, deselect all options.
- ClickSave. Delete is now selectable.
- ClickDelete. A dialog appears.
- In theInstance ID field, enter
quickstart-instance. - ClickDelete.
What's next
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-11-03 UTC.