p:: PostgreSQL

Deployment

Supabase

docker run \
    -e "AUTH_JWT_ALG=HS256" \
    -e "AUTH_JWT_KEY=..." \
    -e "DATABASE_URL=..." \
    -e "DATABASE_USE_IPV6=true" \
    -e "ELECTRIC_WRITE_TO_PG_MODE=direct_writes" \
    -e "PG_PROXY_PASSWORD=..." \
    -e "PG_PROXY_PORT=65432" \
    -p 5133:5133 \
    -p 65432:65432 \
    electricsql/electric

Verify that Electric has successfully initialized its connection to Postgres:

curl localhost:5133/api/status
Connection to Postgres is up!

Fly.io

brew install flyctl
fly auth login

Fly Postgres

fly pg create
? Choose an app name (leave blank to generate one):
automatically selected personal organization: Adithya J
Some regions require a Launch plan or higher (bom, fra).
See https://fly.io/plans to set up a plan.
 
? Select region: Amsterdam, Netherlands (ams)
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
? Scale single node pg to zero after one hour? No
Creating postgres cluster in organization personal
Creating app...
Setting secrets on app silent-surf-4241...
Provisioning 1 of 1 machines with image flyio/postgres-flex:15.6@sha256:92917e5770cef6666dddbf17061c2f95b9e19b8155be9a8ce8c35e09e5381167
Waiting for machine to start...
Machine 2867490be29458 is created
==> Monitoring health checks
  Waiting for 2867490be29458 to become healthy (started, 3/3)
 
Postgres cluster silent-surf-4241 created
  Username:    postgres
  Password:    rUDi4fj22fwBEw4
  Hostname:    silent-surf-4241.internal
  Flycast:     fdaa:0:5fd6:0:1::5
  Proxy port:  5432
  Postgres port:  5433
  Connection string: postgres://postgres:[email protected]:5432
 
Save your credentials in a secure place -- you won't be able to see them again!
 
Connect to postgres
Any app within the Adithya J organization can connect to this Postgres using the above connection string
 
Now that you've set up Postgres, here's what you need to understand: https://fly.io/docs/postgres/getting-started/what-you-should-know/

If you already have an instance of Fly Postgres running, make sure it’s configured with wal_level=logical:

fly pg -a silent-surf-4241 config show
NAME                            VALUE   UNIT    DESCRIPTION                                                                                     PENDING RESTART
shared-buffers                  3200    8kB     Sets the number of shared memory buffers used by the server. (16, 1073741823)                   false
log-statement                   none            Sets the type of statements logged. [none, ddl, mod, all]                                       false
shared-preload-libraries        repmgr          Lists shared libraries to preload into server.                                                  false
wal-level                       replica         Sets the level of information written to the WAL. [minimal, replica, logical]                   false
max-connections                 300             Sets the maximum number of concurrent connections. (1, 262143)                                  false
work-mem                        4096    kB      Sets the maximum memory to be used for query workspaces. (64, 2147483647)                       false
maintenance-work-mem            65536   kB      Sets the maximum memory to be used for maintenance operations. (1024, 2147483647)               false
max-wal-senders                 10              Sets the maximum number of simultaneously running WAL sender processes. (0, 262143)             false
max-replication-slots           10              Sets the maximum number of simultaneously defined replication slots. (0, 262143)                false
log-min-duration-statement      -1      ms      Sets the minimum execution time above which all statements will be logged. (-1, 2147483647)     false
fly pg -a silent-surf-4241 config update --wal-level logical
NAME            VALUE   TARGET VALUE    RESTART REQUIRED
wal-level       replica logical         true
 
? Are you sure you want to apply these changes? Yes
Performing update...
Update complete!
Please note that some of your changes will require a cluster restart before they will be applied.
? Restart cluster now? Yes
Identifying cluster role(s)
  Machine 2867490be29458: primary
Restarting machine 2867490be29458
  Waiting for 2867490be29458 to become healthy (started, 3/3)
Machine 2867490be29458 restarted successfully!
Postgres cluster has been successfully restarted!
fly pg -a silent-surf-4241 config show
NAME                            VALUE   UNIT    DESCRIPTION                                                                                     PENDING RESTART
wal-level                       logical         Sets the level of information written to the WAL. [minimal, replica, logical]                   false
max-connections                 300             Sets the maximum number of concurrent connections. (1, 262143)                                  false
shared-preload-libraries        repmgr          Lists shared libraries to preload into server.                                                  false
max-wal-senders                 10              Sets the maximum number of simultaneously running WAL sender processes. (0, 262143)             false
max-replication-slots           10              Sets the maximum number of simultaneously defined replication slots. (0, 262143)                false
work-mem                        4096    kB      Sets the maximum memory to be used for query workspaces. (64, 2147483647)                       false
maintenance-work-mem            65536   kB      Sets the maximum memory to be used for maintenance operations. (1024, 2147483647)               false
shared-buffers                  3200    8kB     Sets the number of shared memory buffers used by the server. (16, 1073741823)                   false
log-statement                   none            Sets the type of statements logged. [none, ddl, mod, all]                                       false
log-min-duration-statement      -1      ms      Sets the minimum execution time above which all statements will be logged. (-1, 2147483647)     false

Fly Postgres does not support encrypted database connections inside its private 6PN network while Electric enforces encryption by default. The below DATABASE_REQUIRE_SSL setting changes Electric’s behaviour by allowing it to fallback to using unencrypted connections.

Configure your Fly app

The app config needs to include an http_service with internal port 5133 and a TCP service for Electric’s migrations proxy that listens on port 65432 by default.

vim fly.toml
app = "electric-on-fly-test-app-6962"
 
[build]
  image = "electricsql/electric:canary"
 
[env]
  AUTH_MODE = "insecure"
  DATABASE_URL = "postgres://postgres:[email protected]:5432"
  DATABASE_USE_IPV6 = "true"
  DATABASE_REQUIRE_SSL = "false"
  ELECTRIC_WRITE_TO_PG_MODE = "direct_writes"
  PG_PROXY_PASSWORD = "proxy_password"
 
# The main Internet-facing service of Electric
# to which clients will be connecting.
[http_service]
  internal_port = 5133
  force_https = true
 
  [[http_service.checks]]
    interval = "10s"
    timeout = "1s"
    grace_period = "20s"
    method = "GET"
    path = "/api/status"
 
# Service definition for the migrations proxy that runs
# on a separate TCP port.
[[services]]
  protocol = "tcp"
  internal_port = 65432
 
  [[services.ports]]
    port = 65432
    handlers = ["pg_tls"]

As soon as you’re ready to take your Fly app from development to production, make sure to replace the DATABASE_URL, PG_PROXY_PASSWORD and AUTH_JWT_KEY environment variables with secrets.

Deploy

ElectricSQL doesn’t currently support multiple running Electric instances connected to the same database. So it’s important to override Fly’s default behaviour of creating two machines for a new app by passing the --ha=false flag to fly launch.

fly launch --copy-config --ha=false