Skip to content

Commit a9406d7

Browse files
Postgres Bucket Storage Demo (#45)
1 parent d76fcda commit a9406d7

File tree

10 files changed

+204
-3
lines changed

10 files changed

+204
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# PowerSync Self Hosted Example
22

3+
## 2025-01-10
4+
5+
- Added demo for using Postgres as the bucket storage.
6+
37
## 2024-11-27
48

59
- Updated `journeyapps/powersync-service:latest` image specifier which will target `>v.1.0.0` of the Docker image. This version of the Docker image contains support for MongoDB (alpha), MySQL (alpha) and the new modular architecture.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ This repository contains basic demonstrations in the `demos` folder.
3434

3535
- See the README for instructions.
3636

37+
- [Node.js (Postgres + Postgres Sync Bucket Storage)](./demos/nodejs-postgres-bucket-storage/README.md)
38+
39+
- This can be started from the repo root with `docker compose -f demos/nodejs-postgres-bucket-storage/docker-compose.yaml up`
40+
3741
# Config
3842

3943
The configuration can be modified to match other project topologies.

config/powersync.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ replication:
6868
storage:
6969
type: mongodb
7070
uri: !env PS_MONGO_URI
71+
7172
# Use these if authentication is required. The user should have `readWrite` and `dbAdmin` roles
7273
# username: my-mongo-user
7374
# password: my-password

demos/nodejs-custom-checkpoints/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ The `.env` file contains default configuration for the services. Reference this
1111
Ensure you have authenticated with our Docker Image repository. Please reach out to support for an access token.
1212

1313
```bash
14-
docker login container-registry@journeyapps.com -u user
14+
# the value for user doesn't matter
15+
docker login container-registry.journeyapps.com -u user
1516
```
1617

1718
This demo can be started by running the following in this demo directory
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ==================== Postgres credentials ================================
2+
PG_DATABASE_NAME=postgres
3+
PG_DATABASE_PORT=5432
4+
PG_DATABASE_USER=postgres
5+
PG_DATABASE_PASSWORD=postgres
6+
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
7+
8+
9+
PG_STORAGE_DATABASE_NAME=postgres
10+
PG_STORAGE_DATABASE_PORT=5431
11+
PG_STORAGE_DATABASE_USER=postgres
12+
PG_STORAGE_DATABASE_PASSWORD=postgres
13+
PS_STORAGE_SOURCE_URI=postgres://${PG_STORAGE_DATABASE_USER}:${PG_STORAGE_DATABASE_PASSWORD}@pg-storage:${PG_STORAGE_DATABASE_PORT}/${PG_STORAGE_DATABASE_NAME}
14+
15+
# ==================== Demo config =========================================
16+
DEMO_BACKEND_PORT=6060
17+
DEMO_BACKEND_DATABASE_TYPE=postgres
18+
DEMO_BACKEND_DATABASE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
19+
# The front-end demo application is accessible at this port on the host machine
20+
DEMO_CLIENT_PORT=3039
21+
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys
22+
23+
# These can be generated by following the instructions in the `key-generator` folder
24+
# A temporary key will be used if these are not specified
25+
DEMO_JWKS_PUBLIC_KEY=
26+
DEMO_JWKS_PRIVATE_KEY=
27+
28+
# ==================== PowerSync variables ====================
29+
# The PowerSync API is accessible via this port
30+
PS_PORT=8080
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# JavaScript PowerSync + Postgres Bucket Storage
2+
3+
This is a demo for using Postgres as the sync bucket storage driver with PowerSync.
4+
5+
Separate Postgres servers are required for the bucket storage and replication data source.
6+
7+
## Running
8+
9+
The `.env` file contains default configuration for the services. Reference this to connect to any services locally.
10+
11+
This demo can be started by running the following in this demo directory
12+
13+
```bash
14+
docker compose up
15+
```
16+
17+
or in the root directory run
18+
19+
```bash
20+
docker compose -f demos/nodejs-postgres-bucket-storage/docker-compose.yaml up
21+
```
22+
23+
The frontend can be accessed at `http://localhost:3039` in a browser.
24+
25+
The Postgres databases can be accessed at the following URIs
26+
27+
Application data: `postgres://postgres:postgres@localhost:5432/postgres`
28+
29+
bucket storage: `postgres://postgres:postgres@localhost:5431/postgres`
30+
Bucket storage tables are located in the `powersync` schema.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# yaml-language-server: $schema=../schema/schema.json
2+
3+
# Note that this example uses YAML custom tags for environment variable substitution.
4+
# Using `!env [variable name]` will substitute the value of the environment variable named
5+
# [variable name].
6+
#
7+
# Only environment variables with names starting with `PS_` can be substituted.
8+
#
9+
# e.g. With the environment variable `export PS_MONGO_URI=mongodb://localhost:27017`
10+
# and YAML code:
11+
# uri: !env PS_MONGO_URI
12+
# The YAML will resolve to:
13+
# uri: mongodb://localhost:27017
14+
#
15+
# If using VS Code see the `.vscode/settings.json` definitions which define custom tags.
16+
17+
# migrations:
18+
# # Migrations run automatically by default.
19+
# # Setting this to true will skip automatic migrations.
20+
# # Migrations can be triggered externally by altering the container `command`.
21+
# disable_auto_migration: true
22+
23+
# Settings for telemetry reporting
24+
# See https://docs.powersync.com/self-hosting/telemetry
25+
telemetry:
26+
# Opt out of reporting anonymized usage metrics to PowerSync telemetry service
27+
disable_telemetry_sharing: false
28+
29+
# Settings for source database replication
30+
replication:
31+
# Specify database connection details
32+
# Note only 1 connection is currently supported
33+
# Multiple connection support is on the roadmap
34+
connections:
35+
- type: postgresql
36+
# The PowerSync server container can access the Postgres DB via the DB's service name.
37+
# In this case the hostname is pg-db
38+
39+
uri: !env PS_DATA_SOURCE_URI
40+
# SSL settings
41+
sslmode: disable # 'verify-full' (default) or 'verify-ca' or 'disable'
42+
43+
# Connection settings for sync bucket storage
44+
storage:
45+
type: postgresql
46+
# This accepts the same parameters as a postgres data source connection
47+
uri: !env PS_STORAGE_SOURCE_URI
48+
sslmode: disable # 'verify-full' (default) or 'verify-ca' or 'disable'
49+
50+
# The port which the PowerSync API server will listen on
51+
port: !env PS_PORT
52+
53+
# Specify sync rules
54+
sync_rules:
55+
path: sync_rules.yaml
56+
57+
# Client (application end user) authentication settings
58+
client_auth:
59+
# JWKS URIs can be specified here
60+
jwks_uri: !env PS_JWKS_URL
61+
62+
# JWKS audience
63+
audience: ["powersync-dev", "powersync"]
64+
65+
api:
66+
tokens:
67+
# These tokens are used for local admin API route authentication
68+
- use_a_better_token_in_production
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@latest/schema/sync_rules.json
2+
#
3+
# See Documentation for more information:
4+
# https://docs.powersync.com/usage/sync-rules
5+
#
6+
# Note that changes to this file are not watched.
7+
# The service needs to be restarted for changes to take effect.
8+
9+
bucket_definitions:
10+
global:
11+
data:
12+
- SELECT * FROM lists
13+
- SELECT * FROM todos
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Include syntax requires Docker compose > 2.20.3
2+
# https://docs.docker.com/compose/release-notes/#2203
3+
include:
4+
# Demo NodeJS backend server and front-end web client
5+
- path: ../nodejs/ps-nodejs-demo.yaml
6+
7+
services:
8+
# Extend PowerSync with Mongo and Postgres healthchecks
9+
powersync:
10+
extends:
11+
file: ../../services/powersync.yaml
12+
service: powersync
13+
depends_on:
14+
pg-db:
15+
condition: service_healthy
16+
pg-storage:
17+
condition: service_healthy
18+
volumes:
19+
- ./config:/config
20+
environment:
21+
PS_STORAGE_SOURCE_URI: ${PS_STORAGE_SOURCE_URI}
22+
pg-db:
23+
extends:
24+
file: ../../services/postgres.yaml
25+
service: pg-db
26+
volumes:
27+
- ../nodejs/init-scripts:/docker-entrypoint-initdb.d
28+
29+
pg-storage:
30+
image: postgres:latest
31+
restart: always
32+
environment:
33+
- POSTGRES_USER=${PG_STORAGE_DATABASE_USER}
34+
- POSTGRES_DB=${PG_STORAGE_DATABASE_NAME}
35+
- POSTGRES_PASSWORD=${PG_STORAGE_DATABASE_PASSWORD}
36+
- PGPORT=${PG_STORAGE_DATABASE_PORT}
37+
volumes:
38+
- pg_storage_data:/var/lib/postgresql/data
39+
ports:
40+
- "${PG_STORAGE_DATABASE_PORT}:${PG_STORAGE_DATABASE_PORT}"
41+
healthcheck:
42+
test: ["CMD-SHELL", "pg_isready -U ${PG_STORAGE_DATABASE_USER} -d ${PG_STORAGE_DATABASE_NAME}"]
43+
interval: 5s
44+
timeout: 5s
45+
retries: 5
46+
47+
volumes:
48+
pg_storage_data:
49+
pg_data:

demos/supabase/config/powersync.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ replication:
1616

1717
# Connection settings for sync bucket storage
1818
storage:
19-
type: mongodb
20-
uri: !env PS_MONGO_URI
19+
type: postgresql
20+
uri: !env PS_STORAGE_SOURCE_URI
21+
sslmode: disable # 'verify-full' (default) or 'verify-ca' or 'disable'
2122

2223
# The port which the PowerSync API server will listen on
2324
port: !env PS_PORT

0 commit comments

Comments
 (0)