Technical Guides

Docker Compose: run PostgreSQL locally for development

Use Docker Compose to start a repeatable local PostgreSQL service, keep its data in a named volume, and clean it up safely.

Developer ToolsSoftware EngineeringCloud & DevOps

Docker Compose lets you describe a local database service in a small YAML file and start it with one command. It is useful when a development team wants a repeatable PostgreSQL setup without installing the database server directly on every workstation.

The example below is for local development only. It publishes PostgreSQL on the local machine and uses a deliberately obvious placeholder password. Do not reuse these settings for a public or production database.

Create compose.yaml

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_DB: app_dev
      POSTGRES_USER: app
      POSTGRES_PASSWORD: local-dev-only
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

The official PostgreSQL container image reads the initial database and user values on first initialization. The named volume keeps database files between container restarts. Binding the port to 127.0.0.1 keeps it reachable from the local computer without listening on every network interface.

Start and inspect the service

From the directory containing compose.yaml, run:

docker compose up -d
docker compose ps
docker compose logs -f db

When PostgreSQL is ready, connect to 127.0.0.1, port 5432, database app_dev, and user app. A SQL client can use the same values. Keep the terminal log open if startup fails; an occupied port or an old data volume with different initial credentials are common local causes.

Stop it and understand the data

docker compose down

This stops and removes the container but preserves the named volume, so the data is available the next time you run docker compose up -d. To delete the local database data as well, run docker compose down --volumes. That removes the volume and its contents; use it only when you intend to reset the local database.

Why use Compose for a local database?

Compose makes the service configuration reviewable and repeatable. You can pin the image version, document the port, and share setup instructions in the repository. It also makes cleanup explicit. For a team, keep environment-specific secrets out of committed files and document how developers should supply their own credentials.

If you need persistent backups, remote access, high availability, or production security controls, a local Compose file is not a replacement for an operations plan. See the Docker Compose documentation and the PostgreSQL image notes for configuration details.