x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  postgres:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-17.6}
    environment:
      TZ: ${TZ:-UTC}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-postgres}
    volumes:
      - postgres_data:/var/lib/postgresql/data

      # Initialize the database with a custom SQL script
      # - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - '${POSTGRES_PORT_OVERRIDE:-5432}:5432'
    deploy:
      resources:
        limits:
          cpus: ${POSTGRES_CPU_LIMIT:-2.0}
          memory: ${POSTGRES_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${POSTGRES_CPU_RESERVATION:-0.1}
          memory: ${POSTGRES_MEMORY_RESERVATION:-512M}
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  postgres_data:
