This is a basic example of how to sync data from Postgres to SQLite read replica using ElectricSQL.
- Clone the repository
- Run
npm installto install the dependencies - Run
docker compose upto start the Postgres and ElectricSQL servers - Run
npm run syncto start the sync to SQLite process
Run docker compose exec postgres psql -U postgres -d electric to start a psql terminal to the Postgres server. You can use this to add data to the Postgres table.
INSERT INTO items (name, description) VALUES ('Item A', 'Description A');
INSERT INTO items (name, description) VALUES ('Item B', 'Description B');
INSERT INTO items (name, description) VALUES ('Item C', 'Description C');Run sqlite3 local.db to start a sqlite3 terminal to the SQLite database. You can use this to view the data in the SQLite table.
SELECT * FROM items;Ctrl+C in the terminal where the sync is running to stop it.
Run docker compose down to stop the Postgres and ElectricSQL servers and remove the volumes.
The sync maintains the offset in the sync_state table in the SQLite database. If you stop the sync and restart it, it will resume from the same offset.
If you want to reset the sync, you can delete the local.db file and restart the sync.
To clear the Postgres data, you can run docker compose down -v to stop the Postgres and ElectricSQL servers and remove the volumes.