Connect to Cloud SQL for PostgreSQL from Cloud Shell

MySQL  |  PostgreSQL  |  SQL Server

This page shows you how to create and connect to a PostgreSQLinstance and perform basic SQL operations by using the Google Cloud console anda client. The resources created in this quickstart typically cost less than adollar, assuming you complete the steps, including the cleanup, in a timelymanner.

Before you begin

Note:The name you use for your project must be between 4 and 30 characters. When youtype the name, the form suggests a project ID, which you can edit. Theproject ID must be between 6 and 30 characters, with a lowercase letteras the first character. You can use a dash, lowercase letter, or digit for theremaining characters, but the last character cannot be a dash.
  1. Enable the necessary Google Cloud APIs.

    Console

    In the Google Cloud console, go to theAPIs page.

    Go to APIs

    Enable the Cloud SQL Admin API.

    gcloud

    Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run thegcloud commands presented throughout this quickstart.

    Open Cloud Shell

    Run thegcloud services enable command as follows using Cloud Shell to enable the APIs required for this quickstart.:

    gcloudservicesenablesqladmin.googleapis.com

    This command enables the following APIs:

    • Cloud SQL Admin API
  2. Make sure that you have the following role or roles on the project: Cloud SQL Admin (roles/cloudsql.admin), Cloud SQL Viewer (roles/cloudsql.viewer)

    Check for the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. In thePrincipal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check theRole column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to theIAM page.

      Go to IAM
    2. Select the project.
    3. ClickGrant access.
    4. In theNew principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. In theSelect a role list, select a role.
    6. To grant additional roles, clickAdd another role and add each additional role.
    7. ClickSave.

Create a Cloud SQL instance

In this quickstart, you use the Google Cloud console. To use thegcloud CLI, cURL, or PowerShell, seeCreate instances.

  1. In the Google Cloud console, go to theCloud SQL Instances page.

    Go to Cloud SQL Instances

  2. ClickCreate instance.
  3. ClickChoose PostgreSQL.
  4. In theInstance ID field, entermyinstance.
  5. In thePassword field, enter a password for thepostgres user.
  6. ClickCreate instance.

    You're returned to the instances list. You can click the new instance rightaway to see the details, but it won't be available for other operations until it initializes and starts.

    Note: In this example, the instance is created using default settings, including a public IP address.

Connect to your instance

In this quickstart, we'll use the psql client in Cloud Shell to connect to your instance. Cloud Shell is a remote, sandboxed environment.

Note:Cloud Shell doesn't work with a private IP address. These instructions are only for an instance with a public IP address.
  1. Optional: If you're running a local instance of PostgreSQL, stop it beforeconnecting to your Cloud SQL instance. Otherwise, you might encountererrors such asaddress already in use.
  2. In theGoogle Cloud console, click the Cloud Shell icon(Cloud Shell icon.)in the upper right corner.

    When Cloud Shell finishes initializing, a message, such as the following one, appears:

    Welcome to Cloud Shell! Type "help" to get started.Your Cloud Platform project in this session is set to sample-project.Use "gcloud config set project [PROJECT_ID]" to change to a different project.username@sample-project:~ (sample-project)$

  3. Optional: If you haven't authorizedgcloud CLI to access the Google Cloud Platform with your Google user credentials, then use thegcloud auth login command.
  4. At the Cloud Shell prompt, connect to your Cloud SQL instance. Use thegcloud sql connect command as follows. Replace the instance name if your instance name is different.

    gcloudsqlconnectmyinstance--user=postgres
  5. In the dialog, clickAuthorize to authorize Cloud Shell to make API calls.

    The following message appears:

    Allowlisting your IP for incoming connection for 5 minutes...done.

    This message indicates that the public IP address of your Cloud SQL instance is being allowed to have incoming connections. After this message, you're prompted to enter your password.

  6. Enter your postgres password.

    Thepsql prompt appears.

Create a database and upload data

  1. Create a SQL database on your Cloud SQL instance:
    CREATEDATABASEguestbook;
  2. Connect to the database by entering the following command and specifying your password.
    \connectguestbook;
  3. Insert sample data into the database:
    CREATETABLEentries(guestNameVARCHAR(255),contentVARCHAR(255),entryIDSERIALPRIMARYKEY);INSERTINTOentries(guestName,content)values('first guest','I got here!');INSERTINTOentries(guestName,content)values('second guest','Me too!');
  4. Retrieve the data:
    SELECT*FROMentries;
    The result is:
      guestname   |   content   | entryid--------------+-------------+--------- first guest  | I got here! |       1 second guest | Me too!     |       2(2 rows)postgres=>

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. In the Google Cloud console, go to theCloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select themyinstance instance to open theInstance details page.
  3. In the icon bar at the top of the page, clickDelete.
  4. In theDelete instance window, type your instance's name and then clickDelete.

Optional cleanup steps

If you're not using the APIs that were enabled as part of this quickstart, youcan disable them.

  • APIs that were enabled within this quickstart:
    • Cloud SQL Admin API
  1. In the Google Cloud console, go to theAPIs page.

    Go to APIs

  2. Select the Cloud SQL Admin API and then click theDisable API button.

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-07-18 UTC.