Skip to content

Updated

Replicate to an external Postgres

Replicate changes from your Supabase database to a Postgres database outside Supabase, using logical replication with a publication and a subscription.

Prerequisites

For this example, you need:

Procedure

You run commands on both of these databases to publish changes from the Supabase database to the external database.

  1. Create a publication on the Supabase database:

    CREATE PUBLICATION example_pub;
  2. Also on the Supabase database, create a replication slot:

    select pg_create_logical_replication_slot('example_slot', 'pgoutput');
  3. Connect to your external database and subscribe to the publication:

    CREATE SUBSCRIPTION example_sub
    CONNECTION 'host=db.oaguxblfdassqxvvwtfe.supabase.co user=postgres password=YOUR_PASS dbname=postgres'
    PUBLICATION example_pub
    WITH (copy_data = true, create_slot=false, slot_name=example_slot);
  4. Back on the Supabase database, add all the tables that you want replicated to the publication:

    ALTER PUBLICATION example_pub ADD TABLE example_table;
  5. Check the replication status using pg_stat_replication

    select * from pg_stat_replication;

Next steps

To go further with replication, see these pages:

  • Read Replicas: subscribe to a Read Replica instead of the primary on Postgres 17 or later.
  • IPv4 add-on: connect from a replication destination that doesn't support IPv6.