Connect to Cloud SQL for PostgreSQL from Compute Engine

Learn how to deploy a sample app on your Linux or Windows based Compute Engine VM instance connected to a PostgreSQL instance by using the Google Cloud console and a client application.

Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).

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 Cloud APIs necessary to run a Cloud SQL sample app on a Compute Engine VM instance.

    Console

    Click the Enable APIs button to enable the API required for this quickstart.

    Enable APIs

    This enables the following API:

    • Cloud SQL Admin API

    gcloud

    Install the gcloud CLI which provides command-line access to your Google Cloud resources. The gcloud CLI is used to run the gcloud CLI commands presented throughout this quickstart. All the commands are formatted to be run in a terminal or a Powershell window.

    Run the following gcloud command:

    gcloud services enable sqladmin.googleapis.com

    This command enables the following API:

    • Cloud SQL Admin API

Set up Cloud SQL

Create a Cloud SQL instance

Create a database

Console

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

    Go to Cloud SQL Instances

  2. Select quickstart-instance.
  3. Open the Databases tab.
  4. Click Create database.
    1. In the New database dialog box, enter quickstart_db as the name of the database.
    2. Click Create.

gcloud

Run the gcloud sql databases create command to create a database.

gcloud sql databases create quickstart_db --instance=quickstart-instance

Create a user

Console

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

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Select Users from the SQL navigation menu.
  4. Click Add user account.
  5. On the Add a user account to instance instance_name page, add the following information:
    • In the Username field, enter quickstart-user
    • In the Password field, specify a password for your database user. Make a note of this for use in a later step of this quickstart.
  6. Click Add.

gcloud

Before running the following command, make the following replacements:

  1. PASSWORD with a password for your database user. Make a note of this for use in a later step of this quickstart.

Run the gcloud sql users create command to create the user.

gcloud sql users create quickstart-user --instance=quickstart-instance --password=PASSWORD

User name length limits are the same for Cloud SQL as for on-premises PostgreSQL.

Configure a Compute Engine service account

Create and configure a Google Cloud service account that has the Cloud SQL Client role with permissions to connect to Cloud SQL. After you create a service account, you might need to wait for 60 seconds or more before you use the service account.

Console

Create a service account

  1. In the Google Cloud console, go to the Create service account page.
  2. Go to Create service account

  3. Select a Google Cloud project.
  4. Enter a quickstart-service-account as the service account name.
  5. Optional: Enter a description of the service account.
  6. Click Create and continue and continue to the next step.
  7. Choose the Cloud SQL Client role to grant to the service account on the project.
  8. Click Add another role and choose the Storage Object Viewer role to grant to the service account on the project.
  9. Click Continue.
  10. Click Done to finish creating the service account.

gcloud

Create a service account

  1. To create the service account, run the gcloud iam service-accounts create command:
  2. gcloud iam service-accounts create quickstart-service-account --description="DESCRIPTION" --display-name="quickstart-service-account"

    Replace the following value:

  • DESCRIPTION: an optional description of the service account
  • To grant your service account the Cloud SQL Client role and the Storage Object Viewer role on your project, run the gcloud projects add-iam-policy-binding command. Replace PROJECT_ID with your Google Cloud project ID:
    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com" --role="roles/cloudsql.client" --role="roles/storage.objectViewer"
  • Create Compute Engine VM instance

    Create a Compute Engine VM Instance to host a sample web app that connects to Cloud SQL.

    Access Compute Engine VM instance

    Setup development environment for programming language

    Set up the Compute Engine VM instance's development environment for your preferred programming language.

    Install Git

    Install Git, an open source version control system on to your Compute Engine VM instance.

    Clone sample app

    Clone a sample app to your Compute Engine VM instance using the git clone command.

    Go

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Go sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/golang-samples
    2. Change directory to the directory containing the sample app.
      cd golang-samples/cloudsql/postgres/database-sql

    Java

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Java sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/java-docs-samples
    2. Change directory to the directory containing the sample app.
      cd java-docs-samples/cloud-sql/postgres/servlet

    Node.js

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Node.js sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
    2. Change directory to the directory containing the sample app.
      cd nodejs-docs-samples/cloud-sql/postgres/knex

    Python

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Python sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/python-docs-samples
    2. Change directory to the directory containing the sample app.
      cd python-docs-samples/cloud-sql/postgres/sqlalchemy

    Configure and run a Cloud SQL sample app

    With a Cloud SQL instance, database, and service account with client permissions, you can now configure a sample application running on your Compute Engine VM instance to connect to your Cloud SQL instance.

    Clean up

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

    Delete Cloud SQL instance

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

      Go to Cloud SQL Instances

    2. Select the quickstart-instance 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 dialog box, type quickstart-instance, and then click Delete to delete the instance.

    Delete Compute Engine VM instance

    1. In the Google Cloud console, go to the VM instances page.

      Go to VM instances

    2. Select the quickstart-vm-instance instance to open the Instance details page.
    3. In the icon bar at the top of the page, click Delete.
    4. Click Delete to delete the instance.

    Optional cleanup steps

    If you're not using the Cloud SQL client role that you assigned to the Compute Engine default service account, you can remove it.

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

      Go to IAM

    2. Click the edit icon (which looks like a pencil) for the IAM account named Compute Engine default service account.
    3. Delete the Cloud SQL client role.
    4. Click Save.

    If you're not using the API that was enabled as part of this quickstart, you can disable it.

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

      Go to APIs

    2. Select any API that you would like to disable and then click the Disable API button.

    What's next

    Based on your needs, you can learn more about creating Cloud SQL instances.

    You also can learn about creating PostgreSQL users and databases for your Cloud SQL instance.

    For more information about pricing, see Cloud SQL for PostgreSQL pricing.

    Learn more about:

    • Configuring your Cloud SQL instance with a public IP address.
    • Configuring your Cloud SQL instance with a private IP address.

    Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications: