Create and query a Cloud SQL for PostgreSQL database using theGoogle Cloud console

MySQL  |  PostgreSQL  |  SQL Server

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

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud 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.create permission.Learn how to grant roles.
    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.

    Go to project selector

  3. 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.

  4. Verify that billing is enabled for your Google Cloud project.

  5. 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.enable permission.Learn how to grant roles.

    Enable the APIs

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud 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.create permission.Learn how to grant roles.
    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.

    Go to project selector

  7. 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.

  8. Verify that billing is enabled for your Google Cloud project.

  9. 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.enable permission.Learn how to grant roles.

    Enable the APIs

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.

  1. Go to the Cloud SQL Instances page in the Google Cloud console.
    Go to the Cloud SQL Instances page
  2. ClickCreate Instance.
  3. ClickChoose Cloud SQL for PostgreSQL.
  4. ForChoose a Cloud SQL edition, chooseEnterprise.
  5. ForEdition preset, selectSandbox.
  6. ForInstance ID, enterquickstart-instance.
  7. Enter a password for the default user account and save it for future use.
  8. Choose a region near you.
  9. ForZonal availability, selectSingle zone.
  10. ClickCreate instance, and then wait until the instance initializes andstarts. The initialization process can take more than five minutes.
Tip: Check thelong-running operation drawer at the bottom of theGoogle Cloud console to monitor the status of your instance-creationoperation.

Create a database

Afterquickstart-instance has been created, you cancreate a database in your Cloud SQL for PostgreSQL instance.

  1. In the instance navigation menu, clickDatabases.
  2. ClickCreate a database.
  3. In theDatabase Name text field, enterquickstartdb.
  4. 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.

  1. In the instance navigation menu, clickUsers.
  2. ClickAdd user account.
  3. In the pane that opens, selectBuilt-in authentication.
  4. In theUser name field, enterquickstart-user.
  5. Enter a password for the new user. Save this password for future use.
  6. 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.

  1. In the instance navigation menu, clickCloud SQL Studio. A dialog isdisplayed.
  2. In theDatabase drop-down, choosequickstartdb.
  3. SelectBuilt-in database authentication.
  4. In theUser drop-down, selectquickstart-user.
  5. In thePassword field, enter the password that you chose for the user intheAdd a user section.
  6. ClickAuthenticate. Cloud SQL Studio opens.
  7. ClickUntitled Query to open the query editor.
  8. Paste the following code into the query editor:

    CREATESCHEMAIFNOTEXISTS"myschema";
  9. Optional: to correctly format the SQL statement, clickFormat.

  10. ClickRun. The results pane displays a success message.

Create a table

Now create a table using the schema that you created.

  1. ClickNew tab to open a newquery editor tab.
  2. Paste the following statement into the query editor:

    CREATETABLEIFNOTEXISTS"myschema"."quickstart_table"("UserId"SERIALPRIMARYKEY,"FirstName"VARCHAR(255),"LastInitial"VARCHAR(1),"BirthDate"DATE);
  3. Optional: to correctly format the SQL statement, clickFormat.

  4. 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:

  1. ClickNew tab to open a new query editor tab.
  2. 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');
  3. Optional: to format the SQL statement correctly, clickFormat.

  4. 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:

  1. ClickNew tab to open a newquery editor tab.

  2. Paste the following statement into the query editor:

    SELECT*FROM"myschema"."quickstart_table"
  3. 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:

  1. ClickNew tab to open a newquery editor tab.
  2. Paste the following statement into the query editor:

    SELECT"UserId","FirstName"FROM"myschema"."quickstart_table"WHERE"BirthDate" >'1999-12-31';
  3. 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:

  1. In the instance navigation menu, clickOverview.
  2. ClickEdit.
  3. Expand theData Protection section.
  4. InInstance deletion protection, deselect all options.
  5. ClickSave. Delete is now selectable.
  6. ClickDelete. A dialog appears.
  7. In theInstance ID field, enterquickstart-instance.
  8. 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.