mirror of
https://github.com/supabase/supabase.git
synced 2026-07-02 23:24:21 +08:00
230 lines
3.9 KiB
Plaintext
230 lines
3.9 KiB
Plaintext
---
|
|
id: replication
|
|
title: "Replication"
|
|
slug: replication
|
|
---
|
|
|
|
# Replication
|
|
|
|
Replication is technique for copying the data from one database to another. This 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.
|
|
- In Supabase's case, we use the built-in replication functionality to provide a real-time API.
|
|
|
|
|
|
## Publications
|
|
|
|
Publications are a way of choosing which changes should be sent to other systems (usually another Postgres database).
|
|
|
|
|
|
### Managing Publications
|
|
|
|
Supabase provides an interface for managing your publications, or you can use SQL.
|
|
|
|
<Tabs
|
|
defaultActiveId="UI"
|
|
values={[
|
|
{label: 'UI', value: 'UI'},
|
|
{label: 'SQL', value: 'SQL'},
|
|
]}>
|
|
<TabPanel id="UI" label="UI">
|
|
|
|
```sh
|
|
1. Go to the "Database" section.
|
|
2. Click on "Replication" in the sidebar.
|
|
3. Control which database events are sent by toggling the Insert/Update/Delete toggles.
|
|
4. Control which tables broadcast changes by clicking into the "Source" and toggling the tables.
|
|
```
|
|
|
|
<video width="99%" muted playsInline controls="true">
|
|
<source src="/docs/videos/api/api-realtime.mp4" type="video/mp4" muted playsInline />
|
|
</video>
|
|
|
|
</TabPanel>
|
|
<TabPanel id="SQL" label="SQL">
|
|
|
|
```sql
|
|
alter publication supabase_realtime add table products;
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
### Create a Publication
|
|
|
|
This publication will contain all changes to all tables.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create publication publication_name
|
|
for all tables;
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Create a Publication which listens to individual tables
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create publication publication_name
|
|
for table table_one, table_two;
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Add tables to an existing publication
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
alter publication publication_name
|
|
add table table_name;
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Listen to `insert`
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create publication publication_name
|
|
for all tables
|
|
with (publish = 'insert');
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Listen to `update`
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create publication publication_name
|
|
for all tables
|
|
with (publish = 'update');
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Listen to `delete`
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create publication publication_name
|
|
for all tables
|
|
with (publish = 'delete');
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Remove a Publication
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
drop publication if exists publication_name;
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Recreate a Publication
|
|
|
|
If you are planning to re-create a publication, it's best to do it in a transaction to ensure the operation succeeds.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="sql"
|
|
groupId="reference/postgres"
|
|
values={[{ label: 'SQL', value: 'sql' }]}>
|
|
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
begin;
|
|
-- remove the realtime publication
|
|
drop publication if exists publication_name;
|
|
|
|
-- re-create the publication but don't enable it for any tables
|
|
create publication publication_name;
|
|
commit;
|
|
```
|
|
|
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|