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

> Set up Planetscale for Postgres as a source for ClickPipes

# PlanetScale for Postgres source setup guide

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

<Info>
  PlanetScale for Postgres is currently in [early access](https://planetscale.com/postgres).
</Info>

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

ClickPipes supports Postgres version 12 and later.

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

1. To enable replication on your Postgres instance, we need to make sure that the following settings are set:

   ```sql theme={null}
   wal_level = logical
   ```

   To check the same, you can run the following SQL command:

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

   The output should be `logical` by default. If not, please log into the PlanetScale console and go to `Cluster configuration->Parameters` and scroll down to `Write-ahead log` to change it.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/Kmm8X3tBno--Vzxh/images/integrations/data-ingestion/clickpipes/postgres/source/planetscale/planetscale_wal_level_logical.png?fit=max&auto=format&n=Kmm8X3tBno--Vzxh&q=85&s=569c12fad311b207936952616baa32ac" alt="Adjusting wal_level in PlanetScale console" size="md" border width="706" height="260" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/planetscale/planetscale_wal_level_logical.png" />

<Warning>
  Changing this in the PlanetScale console WILL trigger a restart.
</Warning>

2. Additionally, it is recommended to increase the setting `max_slot_wal_keep_size` from its default of 4GB. This is also done via the PlanetScale console by going to `Cluster configuration->Parameters` and then scroll down to `Write-ahead log`. To help determine the new value, please take a look [here](/integrations/clickpipes/postgres/faq#recommended-max_slot_wal_keep_size-settings).

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/Kmm8X3tBno--Vzxh/images/integrations/data-ingestion/clickpipes/postgres/source/planetscale/planetscale_max_slot_wal_keep_size.png?fit=max&auto=format&n=Kmm8X3tBno--Vzxh&q=85&s=b24d175525c34bc92b1fb4e853197a9a" alt="Adjusting max_slot_wal_keep_size in PlanetScale console" size="md" border width="1014" height="286" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/planetscale/planetscale_max_slot_wal_keep_size.png" />

<h2 id="creating-a-user-with-permissions-and-publication">
  Creating a user with permissions and publication
</h2>

Connect to your PlanetScale Postgres instance using the default `postgres.<...>` user and run 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="caveats">
  Caveats
</h2>

1. To connect to PlanetScale Postgres, the current branch needs to be appended to the username created above. For example, if the created user was named `clickpipes_user`, the actual user provided during the ClickPipe creation needs to be `clickpipes_user`.`branch` where `branch` refers to the "id" of the current PlanetScale Postgres [branch](https://planetscale.com/docs/postgres/branching). To quickly determine this, you can refer to the username of the `postgres` user you used to create the user earlier, the part after the period would be the branch id.
2. Don't use the `PSBouncer` port (currently `6432`) for CDC pipes connecting to PlanetScale Postgres, the normal port `5432` must be used. Either port may be used for initial-load only pipes.
3. Please ensure you're connecting only to the primary instance, [connecting to replica instances](https://planetscale.com/docs/postgres/scaling/replicas#how-to-query-postgres-replicas) is currently not supported.

<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.
