> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Set up an AlloyDB Postgres instance as a source for ClickPipes

# AlloyDB Postgres source setup guide

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

<h2 id="supported-versions">
  Supported versions
</h2>

To propagate data from your AlloyDB instance to ClickHouse Cloud using ClickPipes, your instance must be configured for **logical replication**. This is supported **from AlloyDB Version 14**.

<h2 id="enable-logical-replication">
  Enable logical replication
</h2>

To check if logical replication is enabled in your AlloyDB instance, run the following query against your primary instance:

```sql theme={null}
SHOW  wal_level;
```

If the result is `logical`, logical replication is already enabled and you can skip to the [next step](#create-a-clickpipes-user-and-manage-replication-permissions). If the result is `replica`, you must set the [`alloydb.enable_pglogical`](https://cloud.google.com/alloydb/docs/reference/alloydb-flags#alloydb.enable_pglogical) and [`alloydb.logical_decoding`](https://cloud.google.com/alloydb/docs/reference/alloydb-flags#alloydb.logical_decoding) flags to `on` in the primary instance.

<Warning>
  As noted in the [AlloyDB flags documentation](https://cloud.google.com/alloydb/docs/reference/alloydb-flags), modifying the flags that enable logical replication requires a restart of the primary instance.
</Warning>

To enable these flags:

1. In the Google Cloud Console, navigate to the AlloyDB [Clusters](https://console.cloud.google.com/alloydb/clusters) page. From the **Actions** menu for your primary instance, click **Edit**.

   <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/1_edit_instance.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=0cac2b669ef8b0e57332c78e9565104c" alt="Edit primary instance configuration" size="lg" border width="1913" height="649" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/1_edit_instance.png" />

2. Scroll down to **Advanced configuration options** and expand the section. Under **Flags**, click **Add a database flag**.

   * Add the [`allowdb.enable_pglogical`](https://cloud.google.com/alloydb/docs/reference/alloydb-flags#alloydb.enable_pglogical) flag and set its value to `on`
   * Add the [`alloydb.logical_decoding`](https://cloud.google.com/alloydb/docs/reference/alloydb-flags#alloydb.logical_decoding) flag and set its value to `on`

   <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/2_set_flags.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=5124d5b4ae9789c0f4c1067025a27832" alt="Set allowdb.enable_pglogical and alloydb.logical_decoding flags to on" size="lg" border width="1129" height="449" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/2_set_flags.png" />

3. Click **Update instance** to save the configuration changes. It's important to note that this action **triggers a restart of the primary instance.**

4. Once the status of the instance changes from `Updating` to `Ready`, run the following query against your primary instance to verify that logical replication is enabled:

   ```sql theme={null}
   SHOW  wal_level;
   ```

   The result should be `logical`.

   <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/3_verify_logical_replication.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=a54df1337586ad0744e8fafc7f43d26f" alt="Verify that logical replication is enabled" size="lg" border width="1622" height="405" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/3_verify_logical_replication.png" />

<h2 id="create-a-clickpipes-user-and-manage-replication-permissions">
  Create a ClickPipes user and manage replication permissions
</h2>

Connect to your AlloyDB instance as an admin user and execute the following commands:

1. Create a dedicated user for ClickPipes:

   ```sql theme={null}
   CREATE USER clickpipes_user PASSWORD 'some-password';
   ```

2. Grant schema-level, read-only access to the user you created in the previous step. The following example shows permissions for the `public` schema. Repeat these commands for each schema containing tables you want to replicate:

   ```sql theme={null}
   GRANT USAGE ON SCHEMA "public" TO clickpipes_user;
   GRANT SELECT ON ALL TABLES IN SCHEMA "public" TO clickpipes_user;
   ALTER DEFAULT PRIVILEGES IN SCHEMA "public" GRANT SELECT ON TABLES TO clickpipes_user;
   ```

3. Grant replication privileges to the user:

   ```sql theme={null}
   ALTER USER clickpipes_user WITH REPLICATION;
   ```

4. Create a [publication](https://www.postgresql.org/docs/current/logical-replication-publication.html) with the tables you want to replicate. We strongly recommend only including the tables you need in the publication to avoid performance overhead.

<Warning>
  Any table included in the publication must either have a **primary key** defined *or* have its **replica identity** configured to `FULL`. See the [Postgres FAQs](/integrations/clickpipes/postgres/faq#how-should-i-scope-my-publications-when-setting-up-replication) for guidance on scoping.
</Warning>

* To create a publication for specific tables:

  ```sql theme={null}
  CREATE PUBLICATION clickpipes FOR TABLE table_to_replicate, table_to_replicate2;
  ```

* To create a publication for all tables in a specific schema:

  ```sql theme={null}
  CREATE PUBLICATION clickpipes FOR TABLES IN SCHEMA "public";
  ```

The `clickpipes` publication will contain the set of change events generated from the specified tables, and will later be used to ingest the replication stream.

<h2 id="configure-network-access">
  Configure network access
</h2>

<Note>
  ClickPipes doesn't support Private Service Connect (PSC) connections. If you don't allow public access to your AlloyDB instance, you can [use an SSH tunnel](#configure-network-access) to connect securely. PSC will be supported in the future.
</Note>

Next, you must allow connections to your AlloyDB instance from ClickPipes.

<Tabs>
  <Tab title="Allow ClickPipes IPs">
    1. In the Google Cloud Console, navigate to the AlloyDB [Clusters](https://console.cloud.google.com/alloydb/clusters) page. Select your primary instance to open the **Overview** page.

    2. Scroll down to **Instances in your cluster** and click **Edit primary**.

    3. Check the **Enable Public IP** checkbox to allow connections to the instance over the public internet. Under **Authorized external networks**, enter the [list of ClickPipes static IP addresses](/integrations/clickpipes/home#list-of-static-ips) for the region your service is deployed in.

           <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/4_configure_network_security.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=b9ee4403689fd75212d00e153a5b9586" alt="Configure networking for public access with an IP allowlist" size="lg" border width="1515" height="891" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/alloydb/4_configure_network_security.png" />

    <Note>
      AlloyDB expects addresses to be specified in [CIDR notation](https://cloud.google.com/alloydb/docs/connection-overview#public-ip). You can adapt the provided list of ClickPipes static IP addresses to follow this notation by appending `/32` to each address.
    </Note>

    4. Under **Network Security**, select **Require SSL Encryption (default)** (if not already selected).

    5. Click **Update instance** to save the network security configuration changes.
  </Tab>

  <Tab title="Use an SSH tunnel">
    If you don't allow public access to your AlloyDB instance, you must first set up an SSH bastion host to securely tunnel your connection. To set up an SSH bastion host on Google Cloud Platform:

    1. Create and start a Google Compute Engine (GCE) instance following the [official documentation](https://cloud.google.com/compute/docs/instances/create-start-instance).
       * Ensure the GCE instance is in the same Virtual Private Network (VPC) as your AlloyDB instance.
       * Ensure the GCE instance has a [static public IP address](https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address). You’ll use this IP address when connecting ClickPipes to your SSH bastion host.

    2. Update the firewall rules of the SSH bastion host to allow traffic from the [list of ClickPipes static IP addresses](/integrations/clickpipes/home#list-of-static-ips) for the region your service is deployed in.

    3. Update the firewall rules of AlloyDB to allow traffic from the SSH bastion host.
  </Tab>
</Tabs>

<h2 id="whats-next">
  What's next?
</h2>

You can now [create your ClickPipe](/integrations/clickpipes/postgres/index) and start ingesting data from your Postgres instance into ClickHouse Cloud.
Make sure to note down the connection details you used while setting up your Postgres instance as you will need them during the ClickPipe creation process.
