Database

Replicate to another Postgres database using Logical Replication

For this example, you will need:

  • A Supabase project
  • A PostgreSQL database (running v10 or newer)

You will be running 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:

_10
CREATE PUBLICATION example_pub;

  1. Also on the Supabase database, create a replication slot:

_10
select pg_create_logical_replication_slot('example_slot', 'pgoutput');

  1. Now we will connect to our external database and subscribe to our publication Note: ):

_10
CREATE SUBSCRIPTION example_sub
_10
CONNECTION 'host=db.oaguxblfdassqxvvwtfe.supabase.co user=postgres password=YOUR_PASS dbname=postgres'
_10
PUBLICATION example_pub
_10
WITH (copy_data = true, create_slot=false, slot_name=example_slot);

  1. Add all the tables that you want replicated to the publication.

_10
ALTER PUBLICATION example_pub ADD TABLE example_table;

  1. Check the replication status using pg_stat_replication

_10
select * from pg_stat_replication;