In this tutorial, we’ll walk you through how to connect a Google CloudSQL PostgreSQL instance to Stitch as a destination.

For more info on Google CloudSQL’s features and limitations, check out the official Google documentation.


Prerequisites

  • An up-and-running, publicly accessible CloudSQL PostgreSQL instance. Instructions for creating a CloudSQL PostgreSQL destination are outside the scope of this tutorial; our instructions assume that you have an instance up and running. For help getting started with CloudSQL PostgreSQL, refer to Google’s documentation.

    Note: The instance you want to connect must be both publicly accessible and running PostgreSQL version 9.3 or higher.

  • An existing, billing-enabled Cloud Platform project that houses the instance. Even if you’re using the Free option, billing must be enabled for the project or Stitch will encounter connection issues.

    Selecting a project can be done in the Projects page of the Google Console.

  • Access to the CloudSQL Administration API for the Cloud Platform Project housing the instance. Refer to Google’s documentation for more info.

  • Database privileges that allow you to create users and grant privileges. This is required to create a database user for Stitch.


Step 1: Configure database connection settings

For Stitch to successfully connect with your CloudSQL instance, you’ll need to add our IP addresses to the database’s authorized networks list.

Authorized networks with Stitch IP addresses highlighted in the Google Cloud SQL database connections tab

  1. Sign into your Google Cloud Platform account.
  2. Navigate to the Cloud SQL Instances page.
  3. Click the instance name to open its details page.
  4. Click the Connections tab.
  5. Locate the Public IP section.
  6. For each of the following IP addresses, complete the following:

    1. Click + Add network.
    2. In the Name field, enter a name for the IP address. For example: Stitch 1 for the first IP address, Stitch 2 for the second, and so on.
    3. In the Network field, paste one of the following IP addresses:

      • 52.23.137.21/32

      • 52.204.223.208/32

      • 52.204.228.32/32

      • 52.204.230.227/32

    4. Click Done.
    5. Repeat these steps until all of Stitch’s IP addresses have been added.
  7. When finished, click Save to update the instance.

Step 2: Create a CloudSQL PostgreSQL Stitch user

Configuring the second part of the access control settings requires creating a database user for Stitch. This guide will use the psql method to create the user, which requires the use of a SQL client.

  1. If you haven’t already, connect to your CloudSQL PostgreSQL instance using your SQL client.

  2. After connecting, run this command to create a user named stitch. Replace <password> with a secure password:

    CREATE USER stitch WITH PASSWORD '<password>';
  3. Next, you’ll assign the CREATE permissions to the Stitch user. For <database_name>, enter the name of the database where all Stitch-replicated data should be loaded.

    Note: This must be a pre-existing database.

    GRANT CREATE ON DATABASE <database_name> TO stitch
  4. If you restricted access to the system tables, you’ll also need to run the following commands to grant the Stitch user SELECT permissions.

    GRANT SELECT ON ALL TABLES IN SCHEMA information_schema TO stitch
    
    GRANT SELECT ON ALL TABLES IN SCHEMA pg_catalog TO stitch

In the table below are the database user privileges Stitch requires to connect to and load data into CloudSQL PostgreSQL.

Privilege name Reason for requirement
CREATE ON DATABASE

Required to create the necessary database objects to load and store your data.

CREATE permissions on the database are required to successfully load data. When Stitch loads data, it will run a CREATE SCHEMA IF NOT EXISTS command, which will create a schema if it doesn’t already exist. To run this command, the Stitch user must have the CREATE ON DATABASE permission.

Note: The CREATE ON SCHEMA permission is not a sufficient alternative for CREATE ON DATABASE. As outlined in CloudSQL PostgreSQL’s documentation, this permission only allows a user to create objects within a schema, but not the schema itself.

SELECT ON ALL TABLES IN information_schema

Required to select rows from tables in the information_schema schema. Prior to loading data, Stitch will use the data in this schema to verify the existence and structure of integration schemas and tables.

Note: Stitch will only ever read data from systems tables.

SELECT ON ALL TABLES IN pg_catalog

Required to select rows from tables in the pg_catalog schema. Prior to loading data, Stitch will use the data in this schema to verify the existence and structure of integration schemas and tables.

Note: Stitch will only ever read data from systems tables.


Step 3: Connect Stitch

To complete the setup, you need to enter your CloudSQL PostgreSQL connection details into the Destination Settings page in Stitch.

Step 3.1: Locate the connection details in the Google Console

Google CloudSQL PostgreSQL Public IP address field

In this step, you’ll locate the CloudSQL PostgreSQL database’s public IP address in the Google Cloud Platform console. This will be used to complete the setup in Stitch.

  1. In the CloudSQL Instances page, locate the instance you want to connect to Stitch.
  2. When the instance’s Overview page displays, scroll down to the Connect to this instance section.
  3. Locate the Public IP address field.
  4. Copy and paste the public IP address into a text file or leave this page open and open your Stitch account in another tab.

Step 3.2: Enter connection details into Stitch

  1. If you aren’t signed into your Stitch account, sign in now.
  2. Click the Destination tab.

  3. Locate and click the PostgreSQL icon.
  4. Fill in the fields as follows:

    • Host (Endpoint): Enter the host address (endpoint) of your CloudSQL PostgreSQL instance. This will be the value of the Public IP address that you retrieved in the previous step.

    • Port: Enter the port used by the instance. The default is 5432.

    • Username: Enter the Stitch CloudSQL PostgreSQL database user’s username.

    • Password: Enter the password for the Stitch CloudSQL PostgreSQL database user.

    • Database: Enter the name of the CloudSQL PostgreSQL database you want to connect to Stitch.

Step 3.3: Save the destination

When finished, click Check and Save.

Stitch will perform a connection test to the CloudSQL PostgreSQL database; if successful, a Success! message will display at the top of the screen. Note: This test may take a few minutes to complete.


Questions? Feedback?

Did this article help? If you have questions or feedback, feel free to submit a pull request with your suggestions, open an issue on GitHub, or reach out to us.