Database

Replication

Replication is a technique for copying the data from one database to another. Supabase uses replication functionality to provide a real-time API. Replication is useful for:

  • Spreading out the "load." For example, if your database has a lot of reads, you might want to split it between two databases.
  • Reducing latency. For example, you may want one database in London to serve your European customers, and one in New York to serve the US.

Replication is done through publications, a method of choosing which changes to send to other systems (usually another Postgres database). Publications can be managed in the Dashboard or with SQL.

Manage publications in the dashboard

  1. Go to the Database page in the Dashboard.
  2. Click on Publications in the sidebar.
  3. Control which database events are sent by toggling Insert, Update, and Delete.
  4. Control which tables broadcast changes by selecting Source and toggling each table.

Create a publication

This publication contains changes to all tables.


_10
create publication publication_name
_10
for all tables;

Create a publication to listen to individual tables


_10
create publication publication_name
_10
for table table_one, table_two;

Add tables to an existing publication


_10
alter publication publication_name
_10
add table table_name;

Listen to insert


_10
create publication publication_name
_10
for all tables
_10
with (publish = 'insert');

Listen to update


_10
create publication publication_name
_10
for all tables
_10
with (publish = 'update');

Listen to delete


_10
create publication publication_name
_10
for all tables
_10
with (publish = 'delete');

Remove a publication


_10
drop publication if exists publication_name;

Recreate a publication

If you're recreating a publication, it's best to do it in a transaction to ensure the operation succeeds.


_10
begin;
_10
-- remove the realtime publication
_10
drop publication if exists publication_name;
_10
_10
-- re-create the publication but don't enable it for any tables
_10
create publication publication_name;
_10
commit;