# Cloud SQL for PostgreSQL

The PostgreSQL connector type lets you insert, read, update, and delete rows in a PostgreSQL database.

## Supported versions

- PostgreSQL versions 7.4 and later
- TimescaleDB

## Before you begin

Before using the Cloud SQL for PostgreSQL connector, do the following tasks:

- In your Google Cloud project:
  - Ensure that network connectivity is set up. For information about network patterns, see [Network connectivity](https://docs.cloud.google.com/integration-connectors/docs/network-connectivity-private.md.txt).
  - Grant the [roles/connectors.admin](https://docs.cloud.google.com/integration-connectors/docs/connectors-access-permissions.md.txt#iam-roles-for-integration-connectors) IAM role to the user configuring the connector.
  - Grant the following IAM roles to the service account that you want to use for the connector:
    - `roles/secretmanager.viewer`
    - `roles/secretmanager.secretAccessor`
    - `roles/cloudsql.editor`

    A service account is a special type of Google account intended to represent a non-human
    user that needs to authenticate and be authorized to access data in Google APIs.
    Create a service account for the connector, if you haven't already created one. The connector and the service account must belong to the same project. For more
    information, see
    [Creating a service account](https://docs.cloud.google.com/iam/docs/creating-managing-service-accounts#creating).

  > [!NOTE]
  > **Note:** When you're using a service account from a different Google Cloud project than the one where your connection is configured, you might encounter a `PERMISSION_DENIED` error with the message "Permission 'iam.serviceAccounts.getAccessToken' denied". This error occurs because the default permissions granted to the Integration Connectors service agent (P4SA) don't extend across project boundaries. To resolve this, in the project that contains the service account, grant the **Service Account Token Creator** role (`roles/iam.serviceAccountTokenCreator`) to the Integration Connectors service agent.

  - Enable the following services:
    - `secretmanager.googleapis.com` (Secret Manager API)
    - `connectors.googleapis.com` (Connectors API)

    To understand how to enable services, see [Enabling services](https://docs.cloud.google.com/service-usage/docs/enable-disable.md.txt#enabling).
  - Enable the `sqladmin.googleapis.com` (Cloud SQL Admin API) service.


  If these services or permissions have not been enabled for your project previously, you are prompted to enable them
  when configuring the connector.

## Configure the connector

A connection is specific to a data source. It
means that if you have many data sources, you must create a separate connection
for each data source. To create a connection, do the following:

1. In the [Cloud console](https://console.cloud.google.com), go to the **Integration Connectors \> Connections** page and then select or create a Google Cloud project.


   [Go to the Connections page](https://console.cloud.google.com/connectors/connections)
2. Click **+ CREATE NEW** to open the **Create Connection** page.
3. In the **Location** section, choose the location for the connection.
   1. **Region** : Select a location from the drop-down list.

      For the list of all the supported regions, see [Locations](https://docs.cloud.google.com/integration-connectors/docs/locations.md.txt).
   2. Click **NEXT**.
4. In the **Connection Details** section, complete the following:
   1. **Connector** : Select **Cloud SQL for PostgreSQL** from the drop down list of available Connectors.
   2. **Connector version**: Select the Connector version from the drop down list of available versions.
   3. In the **Connection Name** field, enter a name for the Connection instance.

      Connection names must meet the following criteria:
      - Connection names can use letters, numbers, or hyphens.
      - Letters must be lower-case.
      - Connection names must begin with a letter and end with a letter or number.
      - Connection names cannot exceed 49 characters.
   4. Optionally, enter a **Description** for the connection instance.
   5. Optionally, enable [Cloud logging](https://docs.cloud.google.com/integration-connectors/docs/view-connector-logs.md.txt), and then select a log level. By default, the log level is set to `Error`.
   6. **Service Account** : Select a service account that has the [required roles](https://docs.cloud.google.com/integration-connectors/docs/connectors/cloudsqlforpostgresql/configure.md.txt#before-you-begin).
   7. Optionally, configure the **Connection node settings** :


      - **Minimum number of nodes**: Enter the minimum number of connection nodes.
      - **Maximum number of nodes**: Enter the maximum number of connection nodes.

      A node is a unit (or replica) of a connection that processes transactions.
      More nodes are required to process more transactions for a connection and conversely,
      fewer nodes are required to process fewer transactions.
      To understand how the nodes affect your connector pricing, see
      [Pricing for connection nodes](https://docs.cloud.google.com/integration-connectors/docs/pricing#pricing-for-connection-nodes). If you don't enter any values, by default
      the minimum nodes are set to 2 (for better availability) and the maximum nodes are set to 50.

   > [!NOTE]
   > **Note:** You can customize the connection node values only if you are a Pay-as-you-go customer.

   8. **Project ID**: The ID of the Google Cloud project where the CloudSQL instance resides.
   9. **Database Region**: The name of the region where the CloudSQL instance resides.
   10. **Instance ID**: The ID of the CloudSQL instance to connect to.
   11. **Database Name**: The name of the Postgresql database to connect to.
   12. **Browsable Schemas** : Enter a comma-separated list of schemas for the connector to access. For example, `SchemaA,SchemaB,SchemaC`.
   13. Optionally, click **+ ADD LABEL** to add a label to the Connection in the form of a key/value pair.
   14. Click **NEXT**.
5. In the **Destinations** section, enter details of the remote host (backend system) you want to connect to.
   1. **Destination Type** : Select a **Destination Type** .
      - To specify the destination hostname or IP address, select **Host address** and enter the address in the **host 1** field.
      - To establish a private connection, select **Endpoint attachment** . In the **Endpoint Attachment** list, select the endpoint attachment created for your backend system.

      > [!NOTE]
      > **Note:** To learn how to create an endpoint attachment, see [Private
      > network connectivity](https://docs.cloud.google.com/integration-connectors/docs/network-connectivity-private). After creating the endpoint attachment, it is visible in the **Endpoint Attachment** list.

      If you want to establish a public connection to your backend systems with additional security, you can
      consider [configuring static outbound
      IP addresses for your connections](https://docs.cloud.google.com/integration-connectors/docs/configure-static-ip), and then configure
      your firewall rules to allowlist only the specific static IP addresses.

      > [!NOTE]
      > **Note:** If you are connecting to a private Cloud SQL instance, configure a SOCKS5 proxy server, and enter the hostname or IP address of the proxy in the **Host address** field.

      To enter additional destinations, click **+ADD DESTINATION**.
   2. Click **NEXT**.
6. In the **Authentication** section, enter the authentication details.
   1. Select an **Authentication type** and enter the relevant details.

      The following authentication types are supported by the Cloud SQL for PostgreSQL connection:
      - Username and password
   2. To understand how to configure these authentication types, see [Configure authentication](https://docs.cloud.google.com/integration-connectors/docs/connectors/cloudsqlforpostgresql/configure.md.txt#configure-auth).
   3. Click **NEXT**.
7. **Review**: Review your connection and authentication details.
8. Click **Create**.

### Configure authentication

Enter the details based on the authentication you want to use.

- **Username and password**
  - **Username**: The Postgresql username to use for the connection.
  - **Password**: Secret Manager Secret containing the password associated with the Postgresql username.

## Entities, operations, and actions


All the Integration Connectors provide a layer of abstraction for the objects of
the connected application. You can access an application's objects only through
this abstraction. The abstraction is exposed to you as entities, operations, and actions.

> [!NOTE]
> **Note:** You can view the entities, operations, and actions of a connector in the [Connectors task](https://docs.cloud.google.com/application-integration/docs/connectors-task).

- **Entity:** An entity can be thought of as an object, or a collection of properties, in the connected application or service. The definition of an entity differs from a connector to a connector. For example, in a database connector, tables are the entities, in a file server connector, folders are the entities, and in a messaging system connector, queues are the entities.

  However, it is possible that a connector doesn't support or have any entities, in which case the
  `Entities` list will be empty.
- **Operation:** An operation is the activity that you can perform on an entity. You can perform any of the following operations on an entity:
  - [List](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#config-prop)
  - [Get](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#config-prop)
  - [Create](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#config-prop)
  - [Update](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#config-prop)
  - [Delete](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#config-prop)

  Selecting an entity from the available list, generates a list of
  operations available for the entity. For a detailed description of the operations, see the Connectors task's
  [entity operations](https://docs.cloud.google.com/application-integration/docs/connectors-task#config-prop).
  However, if a connector doesn't support any of the entity operations, such unsupported
  operations aren't listed in the `Operations` list.
- **Action:** An action is a first class function that is made available to the integration through the connector interface. An action lets you make changes to an entity or entities, and vary from connector to connector. Normally, an action will have some input parameters, and an output parameter. However, it is possible that a connector doesn't support any action, in which case the `Actions` list will be empty.

> [!NOTE]
> **Note:** All entities and actions will have a schema associated with them. For example, an action schema will have the parameter details such as; the parameter names, and its corresponding data type. The schema (metadata) for entities and actions is fetched by the connection at runtime from your backend. If there are any updates to the schema, such updates won't be automatically reflected in your existing connections; you must manually refresh the schema. To refresh the schema for a connection, open the **Connection details** page of the connection, and then click **Refresh connection schema**.

## System limitations

The Cloud SQL for PostgreSQL connector can process 9 transaction per second,
per [node](https://docs.cloud.google.com/integration-connectors/docs/pricing#payg),
and [throttles](https://docs.cloud.google.com/integration-connectors/docs/rate-limiting.md.txt) any transactions
beyond this limit.
By default, Integration Connectors allocates 2 nodes (for better availability) for a connection.

For information on the limits applicable to Integration Connectors, see [Limits](https://docs.cloud.google.com/integration-connectors/docs/quotas.md.txt).

> [!NOTE]
> **Note:** The number of [Integration Connectors nodes](https://docs.cloud.google.com/integration-connectors/docs/pricing#pricing-for-connection-nodes) will autoscale dynamically based on your usage. However, if you want to reserve capacity for large volumes without waiting for autoscaling, you can adjust the minimum node value for a connection. More nodes are required to process more transactions for a connection. Conversely, fewer nodes are required if a connection processes fewer transactions. To configure the node values, do the following:
>
> - If you are a pay-as-you-go customer, configure the minimum and maximum node value in the edit connection page.
> - If you are a subscription based customer, [contact support](https://docs.cloud.google.com/integration-connectors/docs/getting-support.md.txt).
>
> The maximum transactions that a node can handle depends on various factors. So, before adjusting the minimum nodes for better throughput,
> it is recommended you check if your backend systems are set up optimally to handle the required traffic.

## Supported data types

The following are the supported data types for this connector:

- BIGINT
- BINARY
- BIT
- BOOLEAN
- CHAR
- DATE
- DECIMAL
- DOUBLE
- FLOAT
- INTEGER
- LONGN VARCHAR
- LONG VARCHAR
- NCHAR
- NUMERIC
- NVARCHAR
- REAL
- SMALL INT
- TIME
- TIMESTAMP
- TINY INT
- VARBINARY
- VARCHAR

## Actions

This connector supports execution of the following actions:

- **User-defined stored procedures and functions.** If you have any stored procedures and functions in your backend, those are listed in the `Actions` column of the `Configure connector task` dialog.
- **Custom SQL queries.** To execute custom SQL queries, the connector provides the **Execute custom query** action.

  <br />


  To create a custom query, follow these steps:
  1. Follow the detailed instructions to [add a connectors task](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#add-a-connectors-task).
  2. When you [configure the connector task](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt#configure-the-connectors-task), in the type of action you want to perform, select **Actions**.
  3. In the **Action** list, select **Execute custom query** , and then click **Done** .


     ![image showing execute-custom-query-action](https://docs.cloud.google.com/static/application-integration/images/execute-custom-query-action.png)

     ![image showing execute-custom-query-action](https://docs.cloud.google.com/static/application-integration/images/execute-custom-query-action.png)
  4. Expand the **Task input** section, and then do the following:
     1. In the **Timeout after** field, enter the number of seconds to wait till the query executes.

        Default value: `180` seconds.
     2. In the **Maximum number of rows** field, enter the maximum number of rows to be returned from the database.

        Default value: `25`.
     3. To update the custom query, click **Edit Custom Script** . The **Script editor** dialog opens.


        ![image showing custom-sql-query](https://docs.cloud.google.com/static/application-integration/images/add-custom-sql-query.png)

        ![image showing custom-sql-query](https://docs.cloud.google.com/static/application-integration/images/add-custom-sql-query.png)
     4. In the **Script editor** dialog, enter the SQL query and click **Save** .

        You can use a question mark (?) in a SQL statement to represent a single parameter that must be specified in the query parameters list. For example, the following SQL query selects all rows from the `Employees` table that matches the values specified for the `LastName` column:

        ```
        SELECT * FROM Employees where LastName=?
        ```

        <br />

        **Note:** Data manipulation language (DML) and data definition language (DDL) statements are supported.
     5. If you've used question marks in your SQL query, you must add the parameter by clicking **+ Add Parameter Name** for each question mark. While executing the integration, these parameters replace the question marks (?) in the SQL query sequentially. For example, if you have added three question marks (?), then you must add three parameters in order of sequence.


        ![image showing add-query-param](https://docs.cloud.google.com/static/application-integration/images/add-query-param.png)

        ![image showing add-query-param](https://docs.cloud.google.com/static/application-integration/images/add-query-param.png)

        To add query parameters, do the following:
        1. From the **Type** list, select the data type of the parameter.
        2. In the **Value** field, enter the value of the parameter.
        3. To add multiple parameters, click **+ Add Query Parameter**.
     6. The **Execute custom query** action does not support array variables.

## Action examples

### Example - Find the greater value

This example shows how to execute a user-defined function. The `find_greater`
function in this example, compares two integers and returns the integer which is greater.

1. In the `Configure connector task` dialog, click `Actions`.
2. Select the `find_greater` action, and then click **Done**.
3. In the **Task Input** section of the **Connectors** task, click `connectorInputPayload` and then enter a value similar to the following in the `Default Value` field:

   ```json
   {
   "$1": 1.0,
   "$2": 5.0
   }
   ```
4. If the action execution is successful, the connector task's `connectorOutputPayload` field will have a value similar to the following:

```json
[{
"bignum": 5.0
}]
```

## Entity operation examples

### Example - List records of an entity

This example lists the records of the `Users` entity.

1. In the `Configure connector task` dialog, click `Entities`.
2. Select `Users` from the `Entity` list.
3. Select the `List` operation, and then click **Done**.
4. In the **Task Input** section of the **Connectors** task, you can set the **filterClause** as per your requirement.

   <br />

   For example, setting the filter clause to
   `employeeCode='5100' and startDate='2010-01-01 00:00:00'`, lists only those records
   whose employeeCode is 5100 and startDate is 2010-01-01 00:00:00.

### Example - Get a single record from an entity

This example fetches a record from the `Users` entity.

1. In the `Configure connector task` dialog, click `Entities`.
2. Select `User` from the `Entity` list.
3. Select the `Get` operation, and then click **Done**.
4. In the **Task Input** section of the **Connectors** task, click **entityId** and then enter `103032` in the **Default Value** field.

   Here, `103032` is the primary key value of the `Users` entity.

### Example - Delete a record from an entity

This example deletes a record from the `Users` entity.

1. In the `Configure connector task` dialog, click `Entities`.
2. Select `Users` from the `Entity` list.
3. Select the `Delete` operation, and then click **Done**.
4. In the **Task Input** section of the **Connectors** task, click **entityId** and then enter `113132` in the **Default Value** field.

   Alternately, if the entity has composite primary keys instead of specifying the **entityId** , you can
   set the **filterClause** . For example, `employeeCode='5100' and startDate='2010-01-01 00:00:00'`.

### Example - Create a record in an entity

This example creates a record in the `Users` entity.

1. In the `Configure connector task` dialog, click `Entities`.
2. Select `Users` from the `Entity` list.
3. Select the `Create` operation, and then click **Done**.
4. In the **Task Input** section of the **Connectors** task, click `connectorInputPayload` and then enter a value similar to the following in the `Default Value` field:

   ```json
   {
   "employeeCode": "5100",
   "startDate": "2010-01-01 00:00:00.0",
   "country": "US"
   }
   ```
5. If the integration is successful, the connector task's `connectorOutputPayload` field will have the response of the create operation.

### Example - Update a record in an entity

This example updates a record in the `Users` entity.

1. In the `Configure connector task` dialog, click `Entities`.
2. Select `Users` from the `Entity` list.
3. Select the `Update` operation, and then click **Done**.
4. In the **Task Input** section of the **Connectors** task, click `connectorInputPayload` and then enter a value similar to the following in the `Default Value` field:

   ```json
   {
   "country": "IN"
   }
   ```
5. In the **Task Input** section of the **Connectors** task, click **entityId** and then enter `113132` in the **Default Value** field.

   Alternately, if the entity has composite primary keys instead of specifying the **entityId** , you can
   set the **filterClause** . For example, `employeeCode='5100' and startDate='2010-01-01 00:00:00'`.
6. If the integration is successful, the connector task's `connectorOutputPayload` field will have the response of the update operation.

## Create connections using Terraform

You can use the [Terraform
resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/integration_connectors_connection) to create a new connection.

To learn how to apply or remove a Terraform configuration, see
[Basic Terraform commands](https://docs.cloud.google.com/docs/terraform/basic-commands.md.txt).

To view a sample terraform template for connection creation, see [sample template](https://docs.cloud.google.com/integration-connectors/docs/sample-terraform-template.md.txt).

When creating this connection by using Terraform, you must set the following variables in your Terraform configuration file:

| Parameter name | Data type | Required | Description |
|---|---|---|---|
| project_id | STRING | True | Project ID of the project that contains the Cloud SQL instance. e.g. myproject. |
| database_region | STRING | True | The cloud region for the instance. e.g. us-central1. |
| instance_id | STRING | True | Database instance ID. This does not include the project ID. e.g. myinstance. |
| database_name | STRING | True | Name of the database in the instance. e.g. mydatabase. |

## Use the Cloud SQL for PostgreSQL connection in an integration

After you create the connection, it becomes available in both
Apigee Integration and Application Integration. You can use the connection
in an integration through the Connectors task.

- To understand how to create and use the Connectors task in Apigee Integration, see [Connectors task](https://docs.cloud.google.com/apigee/docs/api-platform/integration/connectors-task#configure-the-connectors-task).
- To understand how to create and use the Connectors task in Application Integration, see [Connectors task](https://docs.cloud.google.com/application-integration/docs/configure-connectors-task.md.txt).

See also the [Perform CRUD operations on a MySQL database](https://docs.cloud.google.com/integration-connectors/docs/perform-crud-operation-mysql-database.md.txt) tutorial
that shows how to create a MySQL connection and use it in an integration to perform read and write operations.

## Get help from the Google Cloud community

You can post your questions and discuss this connector in the Google Cloud community at [Cloud Forums](https://www.googlecloudcommunity.com/gc/Integration-Services/bd-p/cloud-integration-services).

## What's next

- Understand how to [suspend and resume a connection](https://docs.cloud.google.com/integration-connectors/docs/suspend-resume-connection.md.txt).
- Understand how to [monitor connector usage](https://docs.cloud.google.com/integration-connectors/docs/cloud-monitoring.md.txt).
- Understand how to [view connector logs](https://docs.cloud.google.com/integration-connectors/docs/view-connector-logs.md.txt).