version: '3.9'

services:
  postgresql_primary:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/pg_root_password
    secrets:
      - pg_root_password
    volumes:
      - pg_data:/var/lib/postgresql/data
    networks:
      - database-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        limits:
          memory: 4G
          cpus: '2.0'
        reservations:
          memory: 2G
          cpus: '1.0'
      placement:
        constraints:
          - "node.labels.role==db"
      replicas: 1

volumes:
  pg_data:
    driver: local

secrets:
  pg_root_password:
    external: true

networks:
  database-network:
    external: true
