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:
CREATE PUBLICATION example_pub;
  1. Also on the Supabase database, create a replication slot:
select pg_create_logical_replication_slot('example_slot', 'pgoutput');
  1. Now we will connect to our external database and subscribe to our publication Note: ):
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);
  1. Add all the tables that you want replicated to the publication.
ALTER PUBLICATION example_pub ADD TABLE example_table;
  1. Check the replication status using pg_stat_replication
select * from pg_stat_replication;

We only collect analytics essential to ensuring smooth operation of our services. Learn more