Skip to main content

Sync service

The Electric sync service is an Elixir application that manages active-active replication between your Postgres database and your local apps.

You can run the pre-packaged Docker images published on Docker Hub, or build and run your own Docker image, or run it directly as an Elixir service.

Configuration

The Electric sync service is configured using environment variables. The required variables are:

Plus depending on your Write to PG mode you must either:

  • set ELECTRIC_WRITE_TO_PG_MODE=direct_writes; or
  • set LOGICAL_PUBLISHER_HOST to a hostname that the Postgres can connect to the sync service on

For example:

DATABASE_URL="postgresql://user:password@localhost:5432/electric"
ELECTRIC_WRITE_TO_PG_MODE="direct_writes"
PG_PROXY_PASSWORD="..."

See API -> Sync service for the full list of configuration options.

Docker

Images

Pre-packaged images are available on Docker Hub at electricsql/electric. Run using e.g.:

docker run \
-e "DATABASE_URL=postgresql://..." \
-e "ELECTRIC_WRITE_TO_PG_MODE=direct_writes" \
-e "PG_PROXY_PASSWORD=..." \
-e "AUTH_MODE=insecure" \
-p 5133:5133 \
-p 5433:5433 \
-p 65432:65432 \
-t \
electricsql/electric

Compose

You can deploy the sync service together with a Postgres database in a Docker Compose file. For example, save below contents to a file named compose.yaml:

version: "3.1"

volumes:
pg_data:

services:
pg:
image: postgres:14-alpine
environment:
POSTGRES_PASSWORD: pg_password
command:
- -c
- wal_level=logical
ports:
- 5432:5432
restart: always
volumes:
- pg_data:/var/lib/postgresql/data

electric:
image: electricsql/electric
depends_on:
- pg
environment:
DATABASE_URL: postgresql://postgres:pg_password@pg/postgres
DATABASE_REQUIRE_SSL: false
ELECTRIC_WRITE_TO_PG_MODE: direct_writes
PG_PROXY_PASSWORD: proxy_password
AUTH_MODE: insecure
ports:
- 5133:5133
- 65432:65432
restart: always

Then start the services:

docker compose -f compose.yaml up

Build

See the Makefile for more details but e.g.:

docker build -t electric:local-build .

Then run:

docker run \
-e "DATABASE_URL=postgresql://..." \
-e "ELECTRIC_WRITE_TO_PG_MODE=direct_writes" \
-e "PG_PROXY_PASSWORD=..." \
-e "AUTH_MODE=insecure" \
-p 5133:5133 \
-p 5433:5433 \
-p 65432:65432 \
-t \
electric:local-build

Elixir

See the source code and usage instructions at electric-sql/electric/components/electric.

Make sure you have Elixir 1.15 compiled with Erlang 25 installed.

Build:

mix deps.get
mix compile

Test:

mix test

Release:

MIX_ENV="prod" mix release

Run:

./_build/prod/rel/electric/bin/electric start