Quickstart for Cloud SQL and Cloud Run

This page shows you how to deploy a sample app on Cloud Run connected to a PostgreSQL instance using the Google Cloud Console and a client application. The resources created in this quickstart typically cost less than one dollar (USD), assuming you complete the steps, including the clean up, in a timely manner.

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 Cloud project. Learn how to confirm that billing is enabled for your 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 Cloud project. Learn how to confirm that billing is enabled for your project.

  6. Enable the Google Cloud APIs necessary to run a Cloud SQL sample app on Cloud Run.

    Console

    Click the button below to enable the APIs required for this quickstart.

    Enable APIs

    This enables the following APIs:

    • Compute Engine API
    • Cloud SQL Admin API
    • Cloud Run API
    • Container Registry API
    • Cloud Build 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 following gcloud command using Cloud Shell:

    gcloud services enable compute.googleapis.com sqladmin.googleapis.com run.googleapis.com \
      containerregistry.googleapis.com cloudbuild.googleapis.com

    This command enables the following APIs:

    • Compute Engine API
    • Cloud SQL Admin API
    • Cloud Run API
    • Container Registry API
    • Cloud Build 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 the quickstart-instance to add a database to it.
  3. Select the Databases tab.
  4. Click Create database.
  5. In the New database dialog box, enter quickstart_db as the name of the database.
  6. 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. Click the instance name to open its Overview page.
  3. Select Users from the SQL navigation menu.
  4. Click Add user account.
  5. In the Add a user account to instance instance_name page, add the following information:
    • Username: Set to quickstart-user
    • Password: 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 command below, 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.

Deploy sample app to Cloud Run

Configure a Cloud Run service account

Configure the service account used by Cloud Run so that it has the Cloud SQL Client role with permissions to connect to Cloud SQL.

Console

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

    Go to IAM

  2. For the service account named Compute Engine default service account, click the pencil icon and select ADD ANOTHER ROLE button.
  3. Add the Role named Cloud SQL Client.
  4. Click the Save button.

gcloud

  1. Run the following gcloud command to get a list of your project's service accounts:
    gcloud iam service-accounts list
  2. Copy the EMAIL of the Compute Engine service account.
  3. Run the following command to add the Cloud SQL Client role to Compute Engine service account:
    gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
      --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
      --role="roles/cloudsql.client"

Configure a Cloud SQL sample app

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

Deploy a sample app

The steps to deploy the sample to Cloud Run depend on the type of IP address you assigned to your Cloud SQL instance.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in 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 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.

    You cannot reuse an instance name for about 7 days after an instance is deleted.

  5. In the Google Cloud Console, go to the Cloud Run page.

    Go to Cloud Run

  6. Click the checkbox next to the quickstart-service service name.

  7. Click the Delete button at the top of the Cloud Run page.

Optional cleanup steps

If you're not using the Cloud SQL client role that you assigned to the Compute Engine 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 the Save button.

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

  • APIs that were enabled within this quickstart:
    • Compute Engine API
    • Cloud SQL Admin API
    • Cloud Run API
    • Container Registry API
    • Cloud Build 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.

Also see the Cloud SQL pricing information.

Learn more about:

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