Skip to main content

Running the examples

Source code for the example applications is in electric-sql/electric/examples. Each application has a that you can follow, with the primary steps as follows:

Install dependencies:

npm install

Run the backend services (Postgres + Electric):

npm run backend:start

Apply database migrations:

npm run db:migrate

Use the database schema to generate your type-safe Client:

npm run client:generate

(Or npm run client:watch to monitor for database schema changes and auto-generate the client whenever the schema changes.)

Start the app:

npm run dev

Running your own Postgres

npm run backend:start uses Docker Compose to start a connected Postgres and Electric. To run the Electric sync service on top of an existing Postgres instead, make sure that:

  • your Postgres is up and running and configured with wal_level = 'logical'
  • you don't already have a publication called electric_publication
  • you have a database user with adequate permissions for your write mode -- the simplest being SUPERUSER

Connecting Electric to Postgres

The Electric sync service connects to Postgres using the DATABASE_URL environment variable. Depending on your choice of write mode, Postgres may also need to connect to Electric to consume a logical replication publication.

This is configured using the LOGICAL_PUBLISHER_HOST (and LOGICAL_PUBLISHER_PORT) environment variables:

Postgres | | Electric

See API -> Sync service for more information.