# ================================================
# Urban Lifeline - PostgreSQL 数据库
# ================================================

services:
  postgres:
    image: urban-lifeline-pg:${POSTGRES_VERSION:-latest}
    container_name: urban-lifeline-pg
    restart: unless-stopped
    env_file:
      - .env
    networks:
      - urban-lifeline
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres123456}
      POSTGRES_DB: ${POSTGRES_DB:-urban_lifeline}
      TZ: Asia/Shanghai
      PGTZ: Asia/Shanghai
      PGCLIENTENCODING: UTF8
    volumes:
      # 数据目录
      - ${DATA_ROOT:-./volumes}/data:/var/lib/postgresql/data
      # 日志目录
      - ${LOG_ROOT:-./volumes}/logs:/var/log/postgresql
      # 配置文件目录（可选，用于自定义 postgresql.conf）
      - ${CONFIG_ROOT:-./volumes}/config:/etc/postgresql/conf.d:ro
    command:
      - "postgres"
      - "-c"
      - "max_connections=200"
      - "-c"
      - "shared_buffers=256MB"
      - "-c"
      - "effective_cache_size=768MB"
      - "-c"
      - "maintenance_work_mem=64MB"
      - "-c"
      - "checkpoint_completion_target=0.9"
      - "-c"
      - "wal_buffers=16MB"
      - "-c"
      - "default_statistics_target=100"
      - "-c"
      - "random_page_cost=1.1"
      - "-c"
      - "effective_io_concurrency=200"
      - "-c"
      - "log_timezone=Asia/Shanghai"
      - "-c"
      - "timezone=Asia/Shanghai"
      - "-c"
      - "client_encoding=UTF8"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-urban_lifeline}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 60s

networks:
  urban-lifeline:
    name: urban-lifeline
