Development environment with read replicas¶
In order to test the PostgreSQL read replicas that can be configured, it's needed to run the development environment with read replicas. This guide explains which changes to made specifically.
Docker compose¶
Replace the first part of your docker-compose.dev.yml with the following:
services:
db:
image: pgvector/pgvector:pg18
ports:
- "127.0.0.1:5432:5432"
environment:
POSTGRES_USER: ${DATABASE_USER:-baserow}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD:?}
POSTGRES_DB: ${DATABASE_NAME:-baserow}
PGDATA: /var/lib/postgresql/data
volumes:
- pgdata-primary:/var/lib/postgresql/data
- ./docs/development/init/primary-init.sh:/docker-entrypoint-initdb.d/primary-init.sh:ro
command: >
postgres -c wal_level=replica
-c max_wal_senders=10
-c wal_keep_size=1GB
-c hot_standby=on
-c listen_addresses='*'
networks:
- local
db-replica-1:
image: pgvector/pgvector:pg18
ports:
- "127.0.0.1:5433:5432"
environment:
POSTGRES_USER: ${DATABASE_USER:-baserow}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD:?}
REPLICATION_PASSWORD: ${REPLICATION_PASSWORD:-replicatorpass}
PGDATA: /var/lib/postgresql/data
volumes:
- pgdata-replica-1:/var/lib/postgresql/data
- ./docs/development/init/replica-entrypoint.sh:/scripts/replica-entrypoint.sh:ro
user: postgres
depends_on:
- db
entrypoint: [ "/scripts/replica-entrypoint.sh" ]
networks:
- local
db-replica-2:
image: pgvector/pgvector:pg18
ports:
- "127.0.0.1:5434:5432"
environment:
POSTGRES_USER: ${DATABASE_USER:-baserow}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD:?}
REPLICATION_PASSWORD: ${REPLICATION_PASSWORD:-replicatorpass}
PGDATA: /var/lib/postgresql/data
volumes:
- pgdata-replica-2:/var/lib/postgresql/data
- ./docs/development/init/replica-entrypoint.sh:/scripts/replica-entrypoint.sh:ro
user: postgres
depends_on:
- db
entrypoint: [ "/scripts/replica-entrypoint.sh" ]
networks:
- local
Add the following to the end of the file:
volumes:
pgdata-primary:
pgdata-replica-1:
pgdata-replica-2:
Then execute:
chmod +x ./docs/development/init/primary-init.sh
chmod +x ./docs/development/init/replica-entrypoint.sh
Restart your dev environment and observe that the read-only replications are accessible.
They're available on port 5433 and 5434 with the same password you're using the
same username and password as the writer db.
Saveroom configuration¶
Add the following to .env.docker-dev (when running with just dc-dev) and restart the
dev environment. This configures Saveroom to use the read-only replicas for read queries.
When the backend runs on the host (just dev, .env.local), use localhost with ports
5433 and 5434 instead of the container host names.
PGDATA is pinned because PostgreSQL 18 images default to a versioned data directory and
the replica entrypoint expects /var/lib/postgresql/data.
DATABASE_READ_1_NAME="baserow"
DATABASE_READ_1_USER="baserow"
DATABASE_READ_1_PASSWORD="baserow"
DATABASE_READ_1_HOST="db-replica-1"
DATABASE_READ_1_PORT="5432"
DATABASE_READ_2_NAME="baserow"
DATABASE_READ_2_USER="baserow"
DATABASE_READ_2_PASSWORD="baserow"
DATABASE_READ_2_HOST="db-replica-2"
DATABASE_READ_2_PORT="5432"