version: '3.8'

# n8n Production Deployment with PostgreSQL
# Location: /opt/n8n/docker-compose.yml on LXC 210 (10.10.0.210)
#
# This is the complete docker-compose configuration for n8n.manticorum.com
# Updated: 2025-11-13

services:
  postgres:
    image: postgres:15-alpine
    container_name: n8n-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - n8n-network

  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "5678:5678"
    environment:
      # Database Configuration
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}

      # n8n Configuration
      - N8N_HOST=${N8N_HOST}
      - N8N_PORT=5678
      - N8N_PROTOCOL=${N8N_PROTOCOL}
      - WEBHOOK_URL=${WEBHOOK_URL}
      - GENERIC_TIMEZONE=${TIMEZONE}

      # Security
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_BASIC_AUTH_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_BASIC_AUTH_PASSWORD}

      # Performance
      - NODE_ENV=production
      - EXECUTIONS_PROCESS=main
      - EXECUTIONS_MODE=regular

      # Logging
      - N8N_LOG_LEVEL=info
      - N8N_LOG_OUTPUT=console

      # Execution Data Management (Optional)
      # Uncomment to enable automatic cleanup:
      # - EXECUTIONS_DATA_PRUNE=true
      # - EXECUTIONS_DATA_MAX_AGE=168  # Delete after 7 days
      # - EXECUTIONS_DATA_SAVE_ON_ERROR=all
      # - EXECUTIONS_DATA_SAVE_ON_SUCCESS=all
      # - EXECUTIONS_DATA_SAVE_MANUAL_EXECUTIONS=true

    volumes:
      - n8n_data:/home/node/.n8n
    networks:
      - n8n-network

volumes:
  postgres_data:
    name: n8n_postgres_data
  n8n_data:
    name: n8n_data

networks:
  n8n-network:
    name: n8n-network
    driver: bridge
