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

# Supabase Postgres Source Setup Guide on PeerDB Cloud

This is a guide on how to create a supabase PostgreSQL peer which you can use for replication in PeerDB.

<Check>
  PeerDB Cloud supports Supabase via IPv6 natively for seemless replication.
</Check>

<Steps titleSize="h2">
  <Step title="Creating a user with permissions and replication slot">
    Let's create a new user for PeerDB with the necessary permissions suitable for CDC,
    and also create a publication that we'll use for replication. For this, you can head over to the **SQL Editor** for your Supabase Project.

    1. Create a dedicated user for PeerDB:

       1. ```sql theme={null}
               CREATE USER peerdb_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:

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

    3. Grant replication privileges to the user:

       1. ```sql theme={null}
               ALTER USER peerdb_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.

           <Note>
             Any table included in the publication must either have a **primary key** defined *or* have its **replica identity** configured to `FULL`.
           </Note>

       1. To create a publication for specific tables:

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

       2. To create a publication for all tables in a specific schema:

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

       The `peerdb_publication` publication will contain the set of change events generated from the specified tables, and will later be used to create the MIRROR (replication).

    <Frame caption="User and publication commands">
      <img src="https://mintcdn.com/peerdb/wtgPI5cQwjI2FO0m/images/supabase-setup-peerdb-cloud/supabase-commands.jpg?fit=max&auto=format&n=wtgPI5cQwjI2FO0m&q=85&s=bb684fa4de1d4c188e7a330effec551a" width="2672" height="1156" data-path="images/supabase-setup-peerdb-cloud/supabase-commands.jpg" />
    </Frame>

    Click on **Run** to have a publication and a user ready.

    <Note>
      Make sure to replace `peerdb_user` and `peerdb_password` with your desired username and password.

      Also, remember to use the same publication name when creating the mirror in PeerDB.
    </Note>
  </Step>

  <Step title="Create the Supabase Peer on PeerDB Cloud">
    <Note>
      Make sure you are signed in to your [Supabase console](https://supabase.com/) for this step.
    </Note>

    Creating the Supabase Peer in PeerDB Cloud is as simple as granting PeerDB access to your Supabase Project.

    1. Head over to the PeerDB UI and click on **Create Peer**. Select **Supabase** as the source.

    <Frame caption="Select Supabase peer">
      <img src="https://mintcdn.com/peerdb/wtgPI5cQwjI2FO0m/images/supabase-setup-peerdb-cloud/select-supabase-peer.jpg?fit=max&auto=format&n=wtgPI5cQwjI2FO0m&q=85&s=271c5d14e8623d01614289250ba9f252" width="3990" height="2172" data-path="images/supabase-setup-peerdb-cloud/select-supabase-peer.jpg" />
    </Frame>

    2. You should be redirected to Supabase to authorize PeerDB to access your Supabase Project.
       Make sure to select the correct organization from the dropdown and click on **Authorize PeerDB**.

    <Frame caption="Authorize PeerDB">
      <img src="https://mintcdn.com/peerdb/wtgPI5cQwjI2FO0m/images/supabase-setup-peerdb-cloud/authorize-peerdb.jpg?fit=max&auto=format&n=wtgPI5cQwjI2FO0m&q=85&s=e8721a667dcfd92018a6c7dabb0628a2" width="1642" height="1404" data-path="images/supabase-setup-peerdb-cloud/authorize-peerdb.jpg" />
    </Frame>

    3. You should now see a list of your Supabase Projects. Click on the project you want to create the peer for.

    <Frame caption="Select the Supabase Project">
      <img src="https://mintcdn.com/peerdb/wtgPI5cQwjI2FO0m/images/supabase-setup-peerdb-cloud/supabase-app-projects.jpg?fit=max&auto=format&n=wtgPI5cQwjI2FO0m&q=85&s=05b82489fdaa024037102c39bc868c81" width="3990" height="2024" data-path="images/supabase-setup-peerdb-cloud/supabase-app-projects.jpg" />
    </Frame>

    4. Once you select the project, you should see a screen like below with connection details filled in automatically.
       Fill in the username and password you created earlier in the SQL Editor in Step 1.

    <Frame caption="Create the Supabase Peer">
      <img src="https://mintcdn.com/peerdb/wtgPI5cQwjI2FO0m/images/supabase-setup-peerdb-cloud/supabase-peer.jpg?fit=max&auto=format&n=wtgPI5cQwjI2FO0m&q=85&s=ed417b6ab3e233e61f1d5089f572ea30" width="3990" height="2172" data-path="images/supabase-setup-peerdb-cloud/supabase-peer.jpg" />
    </Frame>

    5. Click on **Validate** and once that's green, you can go ahead and click on **Create** to create the peer!

    <Note>
      The PeerDB user must not be restricted by RLS policies, as it can lead to missing data. You can disable RLS policies for the user by running the below command:

      ```sql theme={null}
      ALTER USER peerdb_user BYPASSRLS;
      ```
    </Note>
  </Step>

  <Step title="Increase max_slot_wal_keep_size">
    This is a recommended configuration change to ensure that large transactions/commits do not cause the replication slot to be dropped.

    <Warning>
      This step will restart your Supabase database and may cause a brief downtime.
    </Warning>

    You can increase the `max_slot_wal_keep_size` parameter for your Supabase database to a higher value (at least 100GB or `102400`) by following the [Supabase Docs](https://supabase.com/docs/guides/database/custom-postgres-config#cli-supported-parameters)

    For better recommendation of this value you can contact the PeerDB team.
  </Step>
</Steps>

<Note>
  When creating the Mirror, make sure to reuse the same publication you created earlier in Step 1
</Note>
