Postgres CDC
Realtime's Postgres Change Data Capture (CDC) feature listens for database changes and sends them to clients. Clients are required to subscribe with a JWT dictating which changes they are allowed to receive based on the database's Row Level Security.
Anyone with access to a valid JWT signed with the project's JWT secret is able to listen to your database's changes, unless tables have Row Level Security enabled and policies in place.
Clients can choose to receive INSERT
, UPDATE
, DELETE
, or *
(all) changes for all changes in a schema, a table in a schema, or a column's value in a table. Your clients should only listen to tables in the public
schema and you must first enable the tables you want your clients to listen to.
Postgres CDC works out of the box for tables in the public
schema. You can listen to tables in your private schemas by granting table SELECT
permissions to the database role found in your access token. You can run a query similar to the following:
1GRANT SELECT ON "private_schema"."table" TO authenticated;
caution
We strongly encourage you to enable RLS and create policies for tables in private schemas. Otherwise, any role you grant access to will have unfettered read access to the table.
You can do this in the Replication section in the Dashboard or with the SQL editor:
1begin; 2 -- remove the supabase_realtime publication 3 drop publication if exists supabase_realtime; 4 5 -- re-create the supabase_realtime publication with no tables 6 create publication supabase_realtime; 7commit; 8 9-- add a table to the publication 10alter publication supabase_realtime add table messages;
By default, only new
record changes are sent but if you want to receive the old
record (previous values) whenever you UPDATE
or DELETE
a record,
you can set the replica identity
of your table to full
:
1alter table messages replica identity full;
To listen to all changes in the public
schema:
1const { createClient } = require('@supabase/supabase-js')
2
3const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY)
4
5/*
6 Channel name can be any string.
7 Event name can can be one of:
8 - INSERT
9 - UPDATE
10 - DELETE
11 - *
12*/
13const channel = supabase
14 .channel('schema-db-changes')
15 .on('postgres_changes', { event: '*', schema: 'public' }, (payload) => console.log(payload))
16 .subscribe()
To listen to changes on a table in the public
schema:
1// Supabase client setup
2
3const channel = supabase
4 .channel('table-db-changes')
5 .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, (payload) =>
6 console.log(payload)
7 )
8 .subscribe()
To listen to changes when a column's value in a table matches a client-specified value:
1// Supabase client setup
2
3const channel = supabase
4 .channel('value-db-changes')
5 .on(
6 'postgres_changes',
7 {
8 event: 'UPDATE',
9 schema: 'public',
10 table: 'messages',
11 filter: 'body=eq.hey',
12 },
13 (payload) => console.log(payload)
14 )
15 .subscribe()
To listen to different events and schema/tables/filters combinations with the same channel:
1// Supabase client setup
2
3const channel = supabase
4 .channel('db-changes')
5 .on(
6 'postgres_changes',
7 { event: '*', schema: 'public', table: 'messages', filter: 'body=eq.bye' },
8 (payload) => console.log(payload)
9 )
10 .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'users' }, (payload) =>
11 console.log(payload)
12 )
13 .subscribe()