Skip to content

Commit 821a15f

Browse files
authored
Updates for using Supabase replication role (#134)
* Updates for using Supabase replication role * only need read-only access
1 parent e218481 commit 821a15f

File tree

7 files changed

+71
-185
lines changed

7 files changed

+71
-185
lines changed
Loading

installation/database-setup.mdx

Lines changed: 21 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ description: "Configure your backend database for PowerSync"
44
sidebarTitle: "Overview"
55
---
66

7+
import PostgresPowerSyncUser from '/snippets/postgres-powersync-user.mdx';
8+
import PostgresPowerSyncPublication from '/snippets/postgres-powersync-publication.mdx';
9+
710
## <Icon icon="elephant" iconType="solid" size="24" /> Postgres
811

912
<Check>
@@ -12,11 +15,9 @@ sidebarTitle: "Overview"
1215

1316
Configuring your Postgres database with PowerSync generally involves three tasks:
1417

15-
* Enable logical replication
16-
17-
* Create a PowerSync database user
18-
19-
* Create `powersync` publication
18+
1. Enable logical replication
19+
2. Create a PowerSync database user
20+
3. Create `powersync` publication
2021

2122
We have documented steps for some hosting providers:
2223

@@ -28,17 +29,11 @@ We have documented steps for some hosting providers:
2829

2930
### 2. Create a PowerSync Database User
3031

31-
It is not currently possible to create a new user on Supabase with replication permissions without contacting their support. Instead, use the default `postgres` user.
32+
<PostgresPowerSyncUser />
3233

3334
### 3. Create "powersync" Publication
3435

35-
The `postgres` user does not have access to create a publication for all tables. Instead, list each table explicitly.
36-
37-
This can also be done on the Supabase dashboard in the database *Replication* page.
38-
39-
```sql
40-
CREATE PUBLICATION powersync FOR TABLE public.lists, public.todos;
41-
```
36+
<PostgresPowerSyncPublication />
4237
</Accordion>
4338

4439
<Accordion title="AWS RDS">
@@ -66,32 +61,21 @@ We have documented steps for some hosting providers:
6661

6762
```sql
6863
-- SQL to create powersync user
69-
7064
CREATE ROLE powersync_role WITH LOGIN PASSWORD 'myhighlyrandompassword';
7165

7266
-- Allow the role to perform replication tasks
73-
7467
GRANT rds_replication TO powersync_role;
7568

7669
-- Set up permissions for the newly created role
77-
78-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
79-
70+
-- Read-only (SELECT) access is required
71+
GRANT SELECT ON ALL TABLES IN SCHEMA public TO powersync_role;
8072
```
8173

82-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
83-
8474
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
8575

8676
### 3. Create "powersync" Publication
8777

88-
```sql
89-
-- Create publication to replicate tables. Specify a subset of tables if required.
90-
91-
-- NOTE: this must be named "powersync" at the moment
92-
93-
CREATE PUBLICATION powersync FOR ALL TABLES;
94-
```
78+
<PostgresPowerSyncPublication />
9579
</Accordion>
9680

9781
<Accordion title="Azure Postgres">
@@ -109,34 +93,11 @@ We have documented steps for some hosting providers:
10993

11094
### 2. Create a PowerSync Database User
11195

112-
Create a PowerSync user on Postgres:
113-
114-
```sql
115-
-- SQL to create powersync user
116-
117-
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
118-
119-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
120-
121-
```
122-
123-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
124-
125-
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
96+
<PostgresPowerSyncUser />
12697

12798
### 3. Create "powersync" Publication
12899

129-
```sql
130-
-- Create publication to replicate tables. Specify a subset of tables if required.
131-
132-
-- NOTE: this must be named "powersync" at the moment
133-
134-
CREATE PUBLICATION powersync FOR ALL TABLES;
135-
136-
-- Example for specifying a subset of tables:
137-
138-
-- CREATE PUBLICATION powersync FOR table users, projects, user_projects, checklists;
139-
```
100+
<PostgresPowerSyncPublication />
140101
</Accordion>
141102

142103
<Accordion title="Google Cloud SQL">
@@ -150,30 +111,11 @@ We have documented steps for some hosting providers:
150111

151112
### 2. Create a PowerSync Database User
152113

153-
Create a PowerSync user on Postgres:
154-
155-
```sql
156-
-- SQL to create powersync user
157-
158-
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
159-
160-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
161-
162-
```
163-
164-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
165-
166-
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
114+
<PostgresPowerSyncUser />
167115

168116
### 3. Create "powersync" Publication
169117

170-
```sql
171-
-- Create publication to replicate tables. Specify a subset of tables if required.
172-
173-
-- NOTE: this must be named "powersync" at the moment
174-
175-
CREATE PUBLICATION powersync FOR ALL TABLES;
176-
```
118+
<PostgresPowerSyncPublication />
177119
</Accordion>
178120

179121
<Accordion title="Neon">
@@ -189,29 +131,11 @@ We have documented steps for some hosting providers:
189131

190132
### 2. Create a PowerSync Database User
191133

192-
Create a PowerSync user on Postgres:
193-
194-
```sql
195-
-- SQL to create powersync user
196-
197-
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
198-
199-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
200-
```
201-
202-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
203-
204-
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
134+
<PostgresPowerSyncUser />
205135

206136
### 3. Create "powersync" Publication
207137

208-
```sql
209-
-- Create publication to replicate tables. Specify a subset of tables if required.
210-
211-
-- NOTE: this must be named "powersync" at the moment
212-
213-
CREATE PUBLICATION powersync FOR ALL TABLES;
214-
```
138+
<PostgresPowerSyncPublication />
215139
</Accordion>
216140

217141
<Accordion title="Fly Postgres">
@@ -233,29 +157,11 @@ We have documented steps for some hosting providers:
233157

234158
### 2. Create a PowerSync Database User
235159

236-
Create a PowerSync user on Postgres:
237-
238-
```sql
239-
-- SQL to create powersync user
240-
241-
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
242-
243-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
244-
```
245-
246-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
247-
248-
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
160+
<PostgresPowerSyncUser />
249161

250162
### 3. Create "powersync" Publication
251163

252-
```sql
253-
-- Create publication to replicate tables. Specify a subset of tables if required.
254-
255-
-- NOTE: this must be named "powersync" at the moment
256-
257-
CREATE PUBLICATION powersync FOR ALL TABLES;
258-
```
164+
<PostgresPowerSyncPublication />
259165
</Accordion>
260166
</AccordionGroup>
261167

@@ -292,29 +198,11 @@ For other providers and self-hosted databases:
292198

293199
### 2. Create a PowerSync Database User
294200

295-
Create a PowerSync user on Postgres:
296-
297-
```sql
298-
-- SQL to create powersync user
299-
300-
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
301-
302-
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO powersync_role;
303-
```
304-
305-
For read-only access, only the `SELECT` privilege is required — `INSERT`, `UPDATE` and `DELETE` can be removed.
306-
307-
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication (as well as for any other publications that may exist).
201+
<PostgresPowerSyncUser />
308202

309203
### 3. Create "powersync" Publication
310204

311-
```sql
312-
-- Create publication to replicate tables. Specify a subset of tables if required.
313-
314-
-- NOTE: this must be named "powersync" at the moment
315-
316-
CREATE PUBLICATION powersync FOR ALL TABLES;
317-
```
205+
<PostgresPowerSyncPublication />
318206
</Accordion>
319207

320208
### Unsupported Hosted Postgres Providers

integration-guides/flutterflow-+-powersync.mdx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sidebarTitle: "Overview"
66

77
import CreateCloudInstance from '/snippets/create-cloud-instance.mdx';
88
import SupabaseConnection from '/snippets/supabase-database-connection.mdx';
9+
import PostgresPowerSyncUser from '/snippets/postgres-powersync-user.mdx';
10+
import PostgresPowerSyncPublication from '/snippets/postgres-powersync-publication.mdx';
911

1012
Used in conjunction with **FlutterFlow**, PowerSync enables developers to build local-first apps that are robust in poor network conditions and that have highly responsive frontends while relying on Supabase for their backend. This guide walks you through configuring PowerSync within your FlutterFlow project that has Supabase integration enabled.
1113

@@ -68,11 +70,7 @@ This guide walks you through building a basic item management app from scratch a
6870
## Configure Supabase
6971

7072
1. Create a new project in Supabase.
71-
72-
2. PowerSync uses the Postgres [Write Ahead Log (WAL)](https://www.postgresql.org/docs/current/wal-intro.html) to replicate data changes in order to keep PowerSync SDK clients up to date.
73-
74-
3. Run the below SQL statement in your **Supabase SQL Editor**:
75-
73+
2. To set up the Postgres database for our demo app, we will create a `lists` table. The demo app will have access to this table even while offline. Run the below SQL statement in your **Supabase SQL Editor**:
7674
```sql
7775
create table
7876
public.lists (
@@ -84,20 +82,10 @@ This guide walks you through building a basic item management app from scratch a
8482
constraint lists_owner_id_fkey foreign key (owner_id) references auth.users (id) on delete cascade
8583
) tablespace pg_default
8684
```
87-
85+
3. PowerSync uses the Postgres [Write Ahead Log (WAL)](https://www.postgresql.org/docs/current/wal-intro.html) to replicate data changes in order to keep PowerSync SDK clients up to date. Run the below SQL statement in your **Supabase SQL Editor** to create a Postgres role/user with replication privileges:
86+
<PostgresPowerSyncUser />
8887
4. Create a Postgres publication using the SQL Editor. This will enable data to be replicated from Supabase so that your FlutterFlow app can download it.
89-
90-
```sql
91-
create publication powersync for table public.lists;
92-
```
93-
94-
<Note>
95-
This is a static list of tables. If you add additional tables to your schema, they must also be added to this publication.
96-
</Note>
97-
98-
<Info>
99-
This guide uses the default `postgres` user in your Supabase account for replicating changes to PowerSync, since elevating custom roles to replication [has been disabled](https://github.com/orgs/supabase/discussions/9314) in Supabase. If you want to use a custom role for this purpose, contact the Supabase support team.
100-
</Info>
88+
<PostgresPowerSyncPublication />
10189

10290
## Configure PowerSync
10391

integration-guides/supabase-+-powersync.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sidebarTitle: Overview
66

77
import CreateCloudInstance from '/snippets/create-cloud-instance.mdx';
88
import SupabaseConnection from '/snippets/supabase-database-connection.mdx';
9+
import PostgresPowerSyncUser from '/snippets/postgres-powersync-user.mdx';
10+
import PostgresPowerSyncPublication from '/snippets/postgres-powersync-publication.mdx';
911

1012
<Frame caption="Video walkthrough of the integration guide.">
1113
<iframe width="1005" height="420" src="https://www.youtube.com/embed/Xg5FTYGPn5U?si=4TjdYEACDR2g98yh" title="YouTube video player" frameborder="0"
@@ -48,7 +50,7 @@ We will follow these steps to get an offline-first 'To-Do List' demo app up and
4850
<Steps>
4951
<Step title="Configure Supabase:">
5052
* Create the demo database schema
51-
* Create the Postgres publication
53+
* Create the Postgres user and publication
5254
</Step>
5355
<Step title="Configure PowerSync:">
5456
* Create connection to Supabase
@@ -67,7 +69,7 @@ Create a new Supabase project (or use an existing project if you prefer) and fol
6769

6870
To set up the Postgres database for our _To-Do List_ demo app, we will create two new tables: `lists` and `todos`. The demo app will have access to these tables even while offline.
6971

70-
Run the below SQL statements in your **Supabase SQL Editor**: (if you get a warning about a "potentially destructive operation", that's a false positive that you can safely ignore.)
72+
Run the below SQL statements in your **Supabase SQL Editor**:
7173

7274
```sql
7375
create table
@@ -97,21 +99,20 @@ create table
9799
) tablespace pg_default;
98100
```
99101

100-
### Create the Postgres Publication
102+
### Create a PowerSync Database User
101103

102104
PowerSync uses the Postgres [Write Ahead Log (WAL)](https://www.postgresql.org/docs/current/wal-intro.html) to replicate data changes in order to keep PowerSync SDK clients up to date.
103105

104-
Run the below SQL statement in your **Supabase SQL Editor**:
106+
Run the below SQL statement in your **Supabase SQL Editor** to create a Postgres role/user with replication privileges:
105107

106-
```sql
107-
-- Create publication for powersync
108+
<PostgresPowerSyncUser />
108109

109-
create publication powersync for table public.lists, public.todos;
110-
```
110+
### Create the Postgres Publication
111+
112+
Run the below SQL statement in your **Supabase SQL Editor** to create a Postgres publication:
113+
114+
<PostgresPowerSyncPublication />
111115

112-
<Info>
113-
**Note:** this guide uses the default `postgres` user in your Supabase account for replicating changes to PowerSync, since elevating custom roles to replication [has been disabled](https://github.com/orgs/supabase/discussions/9314) in Supabase. If you want to use a custom role for this purpose, contact the Supabase support team.
114-
</Info>
115116

116117
## Configuring PowerSync
117118

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```sql
2+
-- Create a publication to replicate tables.
3+
-- Specify a subset of tables to replicate if required.
4+
-- NOTE: this must be named "powersync" at the moment
5+
CREATE PUBLICATION powersync FOR ALL TABLES;
6+
```

snippets/postgres-powersync-user.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```sql
2+
-- Create a role/user with replication privileges for PowerSync
3+
CREATE ROLE powersync_role WITH REPLICATION LOGIN PASSWORD 'myhighlyrandompassword';
4+
-- Set up permissions for the newly created role
5+
-- Read-only (SELECT) access is required
6+
GRANT SELECT ON ALL TABLES IN SCHEMA public TO powersync_role;
7+
```
8+
9+
To restrict read access to specific tables, explicitly list allowed tables for both the `SELECT` privilege, and for the publication mentioned in the next step (as well as for any other publications that may exist).

0 commit comments

Comments
 (0)