Docker Compose + PostgreSQL

Docker Compose runs PostgreSQL from the official image alongside application containers, the standard local development setup and a common single-server deployment.

  • DockerDocker
  • PostgreSQLPostgreSQL
Learning curve
Easy
Status
Mature and maintained
Reviewed
November 2026
Best for
  • Local development databases that match production's PostgreSQL major version
  • Integration tests in CI that need a disposable, real PostgreSQL
  • Prototypes, internal tools and side projects on a single VM
  • Onboarding: one command brings up the app with all its dependencies
Not ideal for
  • Business-critical production database
  • Small production app on one VM

Overview

This stack defines PostgreSQL and the services around it in a `compose.yaml` file and runs them with `docker compose up`: Compose v2, a Docker CLI plugin; the Python-based `docker-compose` v1 is end-of-life. The official `postgres` image is configured through environment variables, init scripts and a named volume for data. Developers use it to get a reproducible database in seconds, and small teams use it to run an app and its database on one server.

What's inside

Compose syntax and the postgres image's environment variables are quick to pick up. The harder parts are PostgreSQL itself (roles, upgrades, backups and tuning) and Docker networking and volumes when something misbehaves.

  • PostgreSQL

    PostgreSQL

    Database

    Stores and queries the data

    Learning: Moderate

    Status: Active

  • Docker

    Docker

    Infrastructure

    Hosts, deploys or connects the system

    Learning: Easy

    Status: Active

How it works

Compose reads `compose.yaml`, creates a project network and volumes, and starts containers in dependency order; the app reaches PostgreSQL by its service name over the internal network while data persists in a named volume.

  1. 1.

    Other to infrastructure

    `docker compose up` merges `compose.yaml` with override files, `.env` and active profiles, creates a project-scoped bridge network and named volumes, and pulls the pinned `postgres` image tag.

  2. 2.

    Infrastructure to database

    On first start with an empty data directory, the image's entrypoint runs `initdb`, creates the role and database from `POSTGRES_USER`, `POSTGRES_PASSWORD` and `POSTGRES_DB`, then runs `.sql` and `.sh` files from `/docker-entrypoint-initdb.d`.

  3. 3.

    Database to infrastructure

    A `pg_isready` healthcheck reports readiness, and services declared with `depends_on` and `condition: service_healthy` wait for it instead of crashing on connection refused.

  4. 4.

    Backend to database

    Application containers connect to `postgres:5432` (the service name) through Docker's embedded DNS; the port only needs publishing to the host for local tools such as psql or a GUI client.

  5. 5.

    Database to infrastructure

    Data is written to a named volume that survives `docker compose down`; only `down -v` or removing the volume deletes it, and backups run through `docker compose exec` with `pg_dump`.

Strengths and drawbacks

Strengths

  • One file describes the environment

    Image tags, environment variables, volumes, healthchecks and dependent services live in a versioned `compose.yaml`, so every developer gets the same PostgreSQL version and extensions.

  • Real PostgreSQL instead of a stand-in

    Tests run against the same engine as production rather than SQLite or an in-memory fake, catching problems with JSONB, constraints, extensions and SQL dialect early.

  • Fast, disposable resets

    `docker compose down -v` followed by `up` gives a clean database in seconds, and seed scripts in `/docker-entrypoint-initdb.d` rebuild a known state.

  • Low operational overhead

    No cluster, scheduler or control plane: just Docker Engine on one host, with `docker compose logs`, `exec` and `ps` covering most day-to-day work.

Drawbacks

  • Single host, no failover

    Compose runs everything on one Docker Engine. If the host dies the database is down; there is no built-in replication, automatic failover or zero-downtime rolling deploy.

  • Major upgrades are not a tag bump

    Changing `postgres:16` to `postgres:17` on an existing volume fails because the on-disk format differs; upgrades need pg_dump/restore or pg_upgrade into a new volume. The default data path also changed with the PostgreSQL 18 images, so check the image docs before mounting volumes.

  • Init settings apply only once

    `POSTGRES_*` variables and `/docker-entrypoint-initdb.d` scripts run only when the data directory is empty; changing the password in `.env` later does not change it in the existing database.

  • Exposed ports and visible secrets

    Publishing `5432:5432` on a server listens on all interfaces, and Docker's iptables rules can bypass host firewalls such as ufw. Passwords in `environment:` show up in `docker inspect`; bind to 127.0.0.1 and use `POSTGRES_PASSWORD_FILE` with Compose secrets.

  • Backups and tuning are manual

    Nothing schedules pg_dump, WAL archiving or vacuum monitoring for you, and Docker's small default `/dev/shm` often needs `shm_size` raised for larger queries.

Scores

Editorial ratings out of 10, comparable across every stack in the guide. They are judgement, not benchmarks. How we score

Speed of building
8.6 out of 10, catalog average 7.7

How quickly a team ships features

Ease of learning
8.6 out of 10, catalog average 7.2

Higher means easier to pick up

Scalability
4.4 out of 10, catalog average 7.6

How far it grows before a redesign

Performance
7.0 out of 10, catalog average 7.5

Speed of the running application

Maintainability
6.6 out of 10, catalog average 7.2

How well the codebase ages

Ecosystem
8.2 out of 10, catalog average 8.2

Libraries, hosting, docs and hiring

Flexibility
5.8 out of 10, catalog average 7.4

How many kinds of product it suits

How it scales

Built for one machine. It scales up with a bigger host, not out; growth beyond that means moving PostgreSQL to a managed service or an orchestrated cluster while Compose stays as the development environment.

Starting outMVP and first users
Developer laptops and CI runners: a `postgres` service with a named volume, a `pg_isready` healthcheck and seed scripts, recreated freely.
GrowingReal traffic, a few engineers
One VM running the app, a reverse proxy and PostgreSQL under Compose with `restart: unless-stopped`, nightly pg_dump to object storage and the database port bound to localhost.
LargeHeavy traffic or data
Compose kept for development; production PostgreSQL moved to a managed service (Amazon RDS, Cloud SQL, Azure Database for PostgreSQL) or a dedicated host with streaming replication.
EnterpriseMany teams and regions
Compose files serve as the local contract for dozens of services and Testcontainers covers CI, while production runs on Kubernetes with an operator such as CloudNativePG or on managed databases.

Where it fits

Common projects, from best fit to worst.

  • Local development environment

    Every developer runs the same PostgreSQL version, extensions and seed data with one command, without installing Postgres on their machine.

    Great fit

  • CI integration tests

    A throwaway database per pipeline run; tmpfs storage and `-c fsync=off` make it start and run fast (never use those settings in production).

    Great fit

  • Small production app on one VM

    Works for low-stakes apps once you add backups, monitoring and restricted ports, but every failure and upgrade is yours to handle.

    Workable

  • Business-critical production database

    Needs replication, point-in-time recovery and failover that Compose does not provide; use a managed service or a Kubernetes operator.

    Poor fit

Alternatives

Similar stacks, and when you would choose them instead.

Sources

Reviewed on November 19, 2026 against the documentation below. Found something out of date? Suggest a correction. Read our methodology.

Stacks that share technologies with Docker Compose + PostgreSQL.