Quickstart: Connect to Cloud SQL for PostgreSQL from Cloud Shell  |  Google Cloud (2024)

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

  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.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the necessary Google Cloud APIs.

    Console

    In the Google Cloud console, go to the APIs 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 the gcloud commands presented throughout this quickstart.

    Open Cloud Shell

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

    gcloud services enable sqladmin.googleapis.com

    This command enables the following APIs:

    • Cloud SQL Admin API

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 the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click Create Instance.
  3. Click Choose PostgreSQL.
  4. Enter myinstance for Instance ID.
  5. Enter a password for the postgres user.
  6. Click Create.

    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.

Connect to your instance

In this quickstart, we'll use the psql client in Cloud Shell to connect to your instance.

  1. Optional: If you're running a local instance of PostgreSQL, stop it beforeconnecting to your Cloud SQL instance. Otherwise, you might encountererrors such as address already in use.
  2. In the Google Cloud console, click the Cloud Shell icon(Quickstart: Connect to Cloud SQL for PostgreSQL from Cloud Shell | Google Cloud (1))in the upper right corner.

    When Cloud Shell finishes initializing, a message, such as the following one, appears: 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. At the Cloud Shell prompt, connect to your Cloud SQL instance. Use the gcloud sql connect command as follows. Replace the instance name if your instance name is different.

    gcloud sql connect myinstance --user=postgres
  4. Click Authorize in the message box to authorize Cloud Shell to make API calls.

    A message indicates that your IP is being allowlisted for incoming connections, after which you're prompted to enter your password.

  5. Enter your postgres password.

    The psql prompt appears.

Create a database and upload data

  1. Create a SQL database on your Cloud SQL instance:
    CREATE DATABASE guestbook;
  2. Connect to the database by entering the following command and specifying your password.
    \connect guestbook;
  3. Insert sample data into the database:
    CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255), entryID SERIAL PRIMARY KEY);INSERT INTO entries (guestName, content) values ('first guest', 'I got here!');INSERT INTO entries (guestName, content) values ('second guest', 'Me too!');
  4. Retrieve the data:
    SELECT * FROM entries;
    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 the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select the myinstance instance to open the Instance details page.
  3. In the icon bar at the top of the page, click Delete.
  4. In the Delete 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 the APIs page.

    Go to APIs

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

What's next

  • Learn about creating Cloud SQL instances.

  • Learn about creating PostgreSQL users and databases for your Cloud SQL instance.

  • See the Cloud SQL pricing information.

  • In this quickstart you connected to the instance by using Cloud Shell.Learn about all of the connectivity options in Cloud SQL.How you connectdepends on your networking configuration, such as if your Cloud SQL instancehas a public or private IP address. See how toconfigure your Cloud SQL instance with a public IP and aprivate IP address.

  • Learn about connecting to a Cloud SQL instance from other Google Cloudapplications:

    • From an application running on the App Engine standard environment
    • From an application running on Compute Engine
    • From an application running on GKE
    • From Cloud Functions
    • From Cloud Run
Quickstart: Connect to Cloud SQL for PostgreSQL from Cloud Shell  |  Google Cloud (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5682

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.