> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sync.cdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL

You can use the PostgreSQL connector from the CData Sync application to capture data
from PostgreSQL and move it to any supported destination. To do so, you need to add
the connector, authenticate to the connector, and complete your connection.

## Add the PostgreSQL Connector

To enable Sync to use data from PostgreSQL, you first must add the
connector, as follows:

<ol>
  <li>Open the **Connections** page of the Sync dashboard.</li>
  <li>Click **Add Connection** to open the **Select Connectors** page.</li>
  <li>Click the **Sources** tab and locate
  the **PostgreSQL** row.</li>

  <li>
    Click the **Configure Connection** icon at the end of that row to open
    the **New Connection** page. This action opens the
    **Add Connection** dialog.

    **Note:** If the **Configure Connection** icon is not
    available, click the **Download Connector** icon to install
    the PostgreSQL connector.
  </li>

  <li>Enter a name for your connection in the **Add Connection** dialog.</li>
  <li>Click **Add** to open the **Settings** tab for your connector.</li>
</ol>

For more information about installing new connectors, see
[Connections](https://docs.sync.cdata.com/26.4/en/connections/connections.md).

## Authenticate to PostgreSQL

After you add the connector, you need to set the required properties.

**Server:** Enter the host name or IP of the server that hosts the PostgreSQL database. The default server value is **localhost**.

CData Sync supports authenticating to PostgreSQL in several ways. Select your authentication method below to proceed to the relevant section that contains the authentication details.

* [**Password**](#password) (default)
* [**Azure Active Directory**](#azure-active-directory)
* [**Azure Service Principal**](#azure-service-principal)
* [**Azure Service Principal Certificate**](#azure-service-principal-certificate)
* [**Azure Password**](#azure-password)
* [**Azure Managed Service Identity**](#azure-managed-service-identity)
* [**Aws IAM Roles**](#aws-iam-roles)
* [**Aws EC2 Roles**](#aws-ec2-roles)
* [**GCP Service Account**](#gcp-service-account)

### Password

To connect with user credentials, specify the following properties:

* **Auth Scheme:** Select **Password**.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.
* **Password:** Enter the password that you use to authenticate to your PostgreSQL account.

### Azure Active Directory

To connect with Azure Active Directory, specify the following properties:

* **Auth Scheme:** Select **AzureAD**.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.
* **Azure Tenant:** Enter the Microsoft Online tenant to which you want to connect. If you do not specify a tenant, CData Sync uses the default tenant.
* **OAuth Client Id:** Enter the client Id that you were assigned when you registered your application with an OAuth authorization server.
* **OAuth Client Secret:** Enter the client secret that you were assigned when you registered your application with an OAuth authorization server.

### Azure Service Principal

To connect with an Azure service principal and client secret, set the following properties:

* **Auth Scheme:** Select **AzureServicePrincipal**.
* **Azure Tenant:** Enter the Microsoft Online tenant to which you want to connect.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.
* **OAuth Client Id:** Enter the client Id that you were assigned when you registered your application with an OAuth authorization server.
* **OAuth Client Secret:** Enter the client secret that you were assigned when you registered your application with an OAuth authorization server.
* (Optional) **Scope:** Specify the scope of your access to the application.

To obtain the OAuth client Id and client secret for your application:

1. Log in to the [Azure portal](https://portal.azure.com).
2. In the left navigation pane, select **All services**. Then, search for and select **App registrations**.
3. Click **New registrations**.
4. Enter an application name and select **Any Azure AD Directory - Multi Tenant**.
5. After you create the application, copy the application (client) Id value that is displayed in the **Overview** section. Use this value as the OAuth client Id.
6. Navigate to the **Certificates & Secrets** section and select **New Client Secret** for the application.
7. Specify the duration and save the client secret. After you save it, the key value is displayed.
8. Copy this value because it is displayed only once. You will use this value as the OAuth client secret.
9. On the **Authentication** tab, make sure to select **Access tokens (used for implicit flows)**.

### Azure Service Principal Certificate

To connect with an Azure service principal and client certificate, set the following properties:

* **Auth Scheme:** Select **AzureServicePrincipalCert**.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.
* **Azure Tenant:** Enter the Microsoft Online tenant to which you want to connect.
* **OAuth JWT Cert:** Enter your Java web tokens (JWT) certificate store.
* **OAuth JWT Cert Type:** Enter the type of key store that contains your JWT Certificate. The default type is **PEMKEY\_BLOB**.
* (Optional) **Scope:** Specify the scope of your access to the application.
* (Optional) **OAuth JWT Cert Password:** Enter the password for your OAuth JWT certificate.
* (Optional) **OAuth JWT Cert Subject:** Enter the subject of your OAuth JWT certificate.

To obtain the OAuth certificate for your application:

1. Log in to the [Azure portal](https://portal.azure.com).
2. In the left navigation pane, select **All services**. Then, search for and select **App registrations**.
3. Click **New registrations**.
4. Enter an application name and select **Any Azure AD Directory - Multi Tenant**.
5. After you create the application, copy the application (client) Id value that is displayed in the **Overview** section. Use this value as the OAuth client Id.
6. Navigate to the **Certificates & Secrets** section and select **Upload certificate**. Then, select the certificate to upload from your local machine.
7. Specify the duration and save the client secret. After you save it, the key value is displayed.
8. Copy this value because it is displayed only once. You will use this value as the OAuth client secret.
9. On the **Authentication** tab, make sure to select **Access tokens (used for implicit flows)**.

### Azure Password

To connect with Azure user credentials, specify the following properties:

* **Auth Scheme:** Select **AzurePassword**.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.
* **Password:** Enter the password that you use to connect to your PostgreSQL account.
* **Azure Tenant:** Enter the Microsoft Online tenant to which you want to connect. If you do not specify a tenant, CData Sync uses the default tenant.

### Azure Managed Service Identity

To connect with Azure Managed Service Identity (MSI), specify the following properties:

* **Auth Scheme:** Select **AzureMSI**.
* **User:** Enter the username that you use to authenticate to your PostgreSQL account.

### AWS IAM Roles

To connect with your IAM user credentials, specify the following properties:

<ul><li>**Auth Scheme:** Select **AwsIAMRoles**.</li>
<li>**User:** Enter the username that you use to authenticate to your PostgreSQL account.</li>
<li>**AWS Access Key:** Enter your Amazon Web Services (AWS) account access key. You can locate this value on your AWS security credentials page.</li>
<li>**AWS Secret Key:** Enter your AWS account secret key. You can locate this value on your AWS security credentials page.</li>
<li>**AWS Role ARN:** Enter the Amazon Resource Name (ARN) for the role with which you want to authenticate.</li>
<li>(Optional) **AWS External Id:** Enter the unique identifier that is required when you assume a role in another account.</li></ul>

### AWS EC2 Roles

When you run CData Sync on an EC2 instance, CData Sync can authenticate by using the IAM role that is assigned to the instance. Select **AwsEC2Roles** for **Auth Scheme** to use that role. No additional properties are required.

### GCP Service Account

To connect with a GCP service account, specify the following properties:

* **Auth Scheme:** Select **GCPServiceAccount**.
* **OAuth JWT Cert:** Enter your Java web tokens (JWT) certificate store.
* **OAuth JWT Cert Type:** Enter the type of key store that contains your JWT Certificate. The default type is **PEMKEY\_BLOB**.
* **OAuth JWT Cert Password** (optional) - Enter the password for your OAuth JWT certificate.
* **OAuth JWT Cert Subject** (optional) - Enter the user subject for which the application is requesting delegated access.

## Complete Your Connection

To complete your connection:

1. Specify these settings:
   * **Database:** Enter the name of the database that to which you want to connect when you connect to the PostgreSQL server.
   * **Port:** Enter the port number of the server that hosts the PostgreSQL database. The default port value is **5432**.
2. Define advanced connection settings on the **Advanced** tab. (In most cases, though, you should not need these settings.)
3. If you authenticate with AzureAD, AzurePassword, AzureMSI, or GCPServiceAccount, click **Connect to PostgreSQL** to connect to your PostgreSQL account.
4. Click **Create & Test** to create your connection.

## Configure Logical Replication

CData Sync can use logical replication that is configured in PostgreSQL to update your destination table incrementally. PostgreSQL uses logical decoding to surface the contents of the write-ahead logs, which track data changes in the database, into a readable format. Those changes are read by Sync and pushed into the destination.

### Enable Logical Replication for CData Sync

To enable logical replications for Sync:

1. Ensure that the `wal_level=logical` parameter is set in your PostgreSQL database. You can set this parameter in the postgresql.conf file.

2. Create a logical replication slot for Sync by submitting the following SELECT statement:

   ```
   SELECT pg_create_logical_replication_slot('cdatasync_replication_slot', 'test_decoding');
   ```

   > [!NOTE]
   >
   > The slot name `cdatasync_replication_slot` is an example slot name. You can substitute any name in its place. You need this slot name when you create your job in Sync. It is important that this slot is used by Sync only because changes are consumed when it reads from the slot.

3. Ensure that the user that connects from Sync has permission to read the replication slot by submitting the following statement.

   ```
   ALTER ROLE <em>PostreSQL-User</em> WITH REPLICATION;
   ```

4. Create a job in Sync with a PostgreSQL source. After you select the **Change Data Capture** option, enter the replication slot name.

### Deletion Behavior

Logical replication enables Sync to track deleted records when the source table contains a primary key. If the source table does not contain a primary key, Sync cannot retrieve deleted records.

## Support for Incremental Replication

The PostgreSQL source supports incremental replication. Incremental replication reduces the workload tremendously and minimizes bandwidth use and latency of synchronization. Moving data in increments offers great flexibility when you are dealing with slow APIs or daily quotas.

You must configure the incremental check column based on the PostgreSQL source columns. An *incremental check column* is either a datetime or integer-based column that Sync uses to identify new or modified records when it replicates data. Therefore, you must configure incremental check on a datetime or integer-based column in your PostgreSQL table for incremental replication to occur.

For details about how to set up incremental replication, see [Incremental Replication](https://docs.sync.cdata.com/26.4/en/getting-started/features.md#incremental-replication).

## More Information

For more information about interactions between CData Sync and PostgreSQL, see
[PostgreSQL Connector for CData Sync](https://cdn.cdata.com/help/FPN/synch/default.htm).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.