Connect to Cloud SQL for PostgreSQL from App Engine flexible environment

Learn how to deploy a sample app on App Engine flexible environment connected to a PostgreSQL instance by using the Google Cloud console and a client app.

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. 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 role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  3. Enable the Cloud APIs necessary to run a Cloud SQL sample app on App Engine flexible environment.

    Console

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

    Enable APIs

    This enables the following APIs:

    • Cloud SQL Admin API
    • Compute Engine API
    • Cloud Build API
    • Cloud Logging API

    By clicking the button, you also enable the Google App Engine Flexible Environment.

    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 appengineflex.googleapis.com sqladmin.googleapis.com \
    compute.googleapis.com cloudbuild.googleapis.com logging.googleapis.com

    This command enables the following APIs:

    • Cloud SQL Admin API
    • Compute Engine API
    • Cloud Build API
    • Cloud Logging API

    By running the above command, you also enable the Google App Engine Flexible Environment.

Set up Cloud SQL

Create a Cloud SQL instance

Create a database

Take the following steps to 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. From the SQL navigation menu, select Databases.
  4. Click  Create database.
    1. In the Database name field of the New database dialog, enter quickstart-db.
    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. Select the quickstart-instance PostgreSQL instance that you created.
  3. From the SQL navigation menu, select Users.
  4. Click Add user account.
  5. In the Add a user account to instance quickstart-instance page, add the following information:
    1. In the User name field, enter quickstart-user.
    2. In the Password field, specify a password for your database user. Make a note of this password 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

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

Deploy a sample app to App Engine flexible environment

Create an App Engine app

Create an App Engine app in your Google Cloud project. This enables the App Engine service, creates a default App Engine app, and creates an App Engine service account that you use to connect to Cloud SQL.

Console

  1. In the Google Cloud console, go to the App Engine page.

    Go to App Engine

  2. Click Create application.
  3. From the Select a region drop-down menu, select us-central.
  4. Click Next.
  5. After the Get started page appears, scroll to the bottom of the page.
  6. Click the I'll do this later link.

gcloud

  1. Run the following gcloud app create command to create an App Engine app:
  2. gcloud app create
  3. When prompted to choose the region where you want your App Engine app located, enter the numeric choice for us-central.