Docker
PgCache is distributed as a Docker image. The container bundles an embedded PostgreSQL instance for the cache database, so there is nothing else to run. For the fastest path to a working proxy, see Getting Started; this page covers production deployment.
Image and Ports
docker run -d -p 5432:5432 -p 9090:9090 pgcache/pgcache \
--upstream postgres://user:password@your-db-host:5432/myapp| Port | Purpose |
|---|---|
| 5432 | PostgreSQL wire protocol — point your application here |
| 9090 | HTTP endpoint for Prometheus metrics and health checks |
Security: the metrics/health port is unauthenticated. Publish it only to trusted/internal networks, not the public internet. See Monitoring.
Checking the Origin
Before the first run, ask the image whether the origin is ready. It connects with the same settings the proxy would use and reports each requirement with a fix:
docker run --rm pgcache/pgcache check \
--upstream postgres://user:password@your-db-host:5432/myapppgcache check: origin user@your-db-host:5432/myapp
PASS connection PostgreSQL 18.1
PASS server version PostgreSQL 18.1
FAIL wal_level wal_level is 'replica'; logical replication needs 'logical'
fix: ALTER SYSTEM SET wal_level = logical;
then restart PostgreSQL (a reload is not enough).
PASS replication slots 0 of 10 replication slots and 0 of 10 wal senders in use
PASS role privileges user has REPLICATION and CREATE on the database
...
WARN primary keys 2 of 17 tables have no primary key and are forwarded, never cached: ...
9 passed, 1 warnings, 1 failed
Origin is not ready for pgcache. Fix the failures above and run the check again.
A failure exits with status 1 and means the proxy would not start or would not be safe to run; a warning means it starts but the named tables are forwarded rather than cached. Remediations are worded for the detected host: self-managed PostgreSQL, Amazon RDS and Aurora, Cloud SQL, or Neon.
The checks are: connection, server version, wal_level, replication slot and WAL sender capacity, role privileges, existing publication and slot, the schema-change signal (a failure with the install SQL when the event triggers are missing, disabled or another version’s and the role cannot install them), a logical replication connection, table ownership, primary keys, views, row-level security, and pg_stat_statements. ALLOWED_TABLES and EXCLUDED_TABLES scope the table checks to the tables you intend to cache.
No embedded PostgreSQL is initialised in check mode, so the command is quick and leaves nothing behind. The same report is available from the binary as pgcache --check.
Docker Compose
services:
pgcache:
image: pgcache/pgcache
restart: always
ports:
- "5432:5432"
- "9090:9090"
environment:
UPSTREAM_URL: postgres://user:password@db:5432/myapp
NUM_WORKERS: 4
PUBLICATION: pgcache_pub
SLOT: pgcache_slot
healthcheck:
test: ["CMD", "pgcache", "--healthcheck", "--metrics_socket", "127.0.0.1:9090"]
interval: 10s
timeout: 3s
retries: 3
Environment variables map to PgCache’s CLI options — see the Configuration reference for the full list.
Mounting a TOML config
For version-controlled configuration, mount a TOML file and point CONFIG_FILE at it:
services:
pgcache:
image: pgcache/pgcache
restart: always
ports:
- "5432:5432"
- "9090:9090"
volumes:
- ./pgcache.toml:/etc/pgcache/config.toml:ro
environment:
CONFIG_FILE: /etc/pgcache/config.toml
The file follows the TOML reference without a [cache] table: the image runs the cache database and points PgCache at it.
An environment variable or CLI argument you set overrides the file’s value, so you can keep a base config in TOML and override per-environment values through the environment. What you leave unset comes from the file: num_workers, [listen] and [cdc] are the file’s, not the image defaults.
Three things stay with the environment:
- Metrics port — the image’s health check probes
METRICS_PORT(default9090), so the file’s[metrics]socket is not used. CDC_SUFFIX— when set, the publication and slot names come fromPUBLICATIONandSLOT(or their defaults) with the suffix appended, not from[cdc].- Secrets —
ORIGIN_PASSWORDandREPLICATION_PASSWORDwork with a config-file origin, so passwords can stay out of the file.
Creating the config file
Instead of writing the file by hand, let PgCache write it. Set CONFIG_CREATE=1 and point CONFIG_FILE at a path on a volume; /etc/pgcache is writable by the image’s user, and so is a named volume mounted there:
services:
pgcache:
image: pgcache/pgcache
restart: always
ports:
- "5432:5432"
- "9090:9090"
volumes:
- pgcache-config:/etc/pgcache
environment:
CONFIG_FILE: /etc/pgcache/config.toml
CONFIG_CREATE: 1
UPSTREAM_URL: postgres://user@db:5432/myapp
ORIGIN_PASSWORD: ${ORIGIN_PASSWORD}
volumes:
pgcache-config:
When nothing is at that path, the first start writes a complete config file from the settings it was given and runs from it. Later starts find the file and behave as with any mounted config: the file decides num_workers, [listen] and [cdc], and what you set in the environment overrides it. Changes made through PUT /config are saved to the same file.
What is written:
- Required settings, always — origin, CDC names, listen address and worker count, as resolved at that first start. The worker count is the one derived from the CPUs allocated then; edit the file or set
NUM_WORKERSafter resizing the container. - Optional settings, only when you set them — one left at its default stays out of the file, so a later release’s default still applies.
- Never passwords — keep
ORIGIN_PASSWORD(andREPLICATION_PASSWORD) in the environment. A password insideUPSTREAM_URLis not written either.
An existing file is never overwritten.
Mounting TLS certificates
To terminate TLS for client connections, mount the certificate and key and reference them in config (tls_cert / tls_key), or pass them as base64-encoded environment variables (PGCACHE_TLS_CERT / PGCACHE_TLS_KEY). See TLS Support.
volumes:
- ./certs/server.crt:/etc/pgcache/server.crt:ro
- ./certs/server.key:/etc/pgcache/server.key:roHealth Checks
The HTTP port exposes two probes for orchestrators and load balancers:
GET /healthz— liveness; returns200whenever the process is running.GET /readyz— readiness; returns200when the cache is running,503otherwise.
The image declares its own HEALTHCHECK against /healthz (10 s interval, 3 s timeout, 3 retries); the compose healthcheck block above overrides it and is only needed to change those values. The image ships neither wget nor curl: pgcache --healthcheck --metrics_socket HOST:PORT probes /healthz itself and exits 0 on a 200, and that is what the image’s HEALTHCHECK runs. Use /healthz for the container healthcheck and /readyz to gate traffic in a load balancer or Kubernetes deployment. See Monitoring → Health Checks.
Restart Policy
Use restart: always (or unless-stopped) so the container recovers from crashes. Note the interaction with the replication slot: PgCache drops its slot when it is stopped (docker stop, docker compose down), but a crash or a kill leaves the slot behind on the origin, where it retains WAL until the container restarts and resumes consuming it. An automatic restart policy keeps that window short. PgCache needs a moment to drop the slot when it stops, well inside Docker’s default 10 second grace period; a container removed with docker rm -f is killed and cleans up nothing. For a container that is gone for good, docker run --rm pgcache/pgcache cleanup --upstream ... removes what it left: see Operations → Removing PgCache from an origin. Understand this behavior — and how to detect a stuck slot — before running in production: see Operations → The Replication Slot.
Resource Sizing
- CPU / workers:
NUM_WORKERSdefaults to about one worker per CPU allocated to the container (minimum 2); set it to override that. - Memory & disk: the cache holds hot data in the embedded PostgreSQL instance. PgCache bounds itself automatically — by a memory budget (80% of detected RAM by default, lower it with
MEMORY_LIMIT) and by cache-volume disk pressure (auto-derived from free space, or cap it withDISK_LIMIT). Give the container enough RAM and disk for your working set. See Configuration → Cache Behavior. - Cold start: the cache is reset on every container start and warms from scratch. For planned cutovers, pre-populate hot queries with
pinned_queries— see Operations → Cache Warm-Up.