yamlbeginner

Docker Compose — PostgreSQL Dev Environment

Full Docker Compose setup for PostgreSQL with pgAdmin, init scripts, volumes, and health checks.

yaml
version: '3.9'

services:
  postgres:
    image: postgres:16-alpine
    container_name: dev-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: ${DB_PASSWORD:-devpass123}
    ports:
      - '5432:5432'
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U devuser -d myapp']
      interval: 10s
      timeout: 5s
      retries: 5

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: dev-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@local.dev
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - '5050:80'
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  pgdata:
    driver: local

Sponsored

DigitalOcean

Use Cases

  • Local database development environment
  • Onboarding new developers with one command
  • Integration testing with real PostgreSQL

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.