Docker Compose for n8n

n8n workflow automation with PostgreSQL. Open-source alternative to Zapier.

n8n automation workflow postgres
compose.yaml
services:
  n8n:
    image: n8nio/n8n:latest
    ports:
      - target: 5678
        mode: ingress
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=postgres
      - DB_POSTGRESDB_PASSWORD=${DB_POSTGRESDB_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
    healthcheck:
      test:
        - CMD
        - curl
        - -f
        - http://localhost:5678/healthz
      interval: 30s
      timeout: 5s
      retries: 3
    depends_on:
      - db
    deploy:
      resources:
        reservations:
          cpus: "0.5"
          memory: 256M
    restart: unless-stopped
  db:
    image: postgres:16
    ports:
      - target: 5432
        mode: host
    environment:
      - POSTGRES_DB=n8n
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -U postgres
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        reservations:
          cpus: "0.5"
          memory: 256M
    restart: unless-stopped
    x-defang-postgres: true

Services

  • n8n n8nio/n8n:latest
  • db postgres:16

Deploy

$ npx defang@latest compose up

Learn more about Defang