# n8n + PostgreSQL + Redis

> Workflow automation platform with 300+ integrations.

- Category: Productivity & Collaboration
- Difficulty: intermediate
- Components: n8n, postgresql, redis
- Tags: n8n, automation, workflow, integrations, zapier-alternative
- Source: https://docker.recipes/productivity/n8n-automation

## Overview

n8n (pronounced n-eight-n) is a fair-code workflow automation platform that serves as a powerful self-hosted alternative to services like Zapier and Microsoft Power Automate. Founded in 2019, n8n provides a visual workflow builder with over 350 pre-built integrations, allowing users to connect different services and automate complex business processes without writing code. Unlike SaaS alternatives, n8n's fair-code licensing model ensures the source code remains accessible while providing enterprise features for commercial use.

This production-ready stack combines n8n with PostgreSQL for robust data persistence and Redis for high-performance queue management. PostgreSQL stores workflow definitions, execution history, and user credentials with ACID compliance, while Redis handles the execution queue system that enables horizontal scaling through worker processes. The configuration includes a separate n8n-worker container that processes workflows asynchronously, preventing the main application from becoming overwhelmed during high-volume automation runs.

This stack is ideal for organizations requiring scalable workflow automation with enterprise-grade reliability. Small to medium businesses benefit from the cost savings compared to per-execution SaaS pricing, while larger enterprises appreciate the data sovereignty and customization options. DevOps teams favor this setup for CI/CD integrations, IT departments use it for system monitoring and alerting workflows, and marketing teams leverage it for lead nurturing and customer communication automation.

## Features

- Visual workflow editor with 350+ pre-built service integrations including Slack, Google Workspace, AWS, and Salesforce
- Queue-based execution system using Redis Bull queues for horizontal scaling and reliable workflow processing
- PostgreSQL database backend providing ACID compliance for workflow definitions, execution history, and credential storage
- Dedicated worker processes for handling long-running workflows without blocking the main application interface
- Webhook triggers supporting real-time workflow initiation from external services and custom applications
- Advanced error handling with configurable retry policies, exponential backoff, and workflow branching based on success/failure
- Credential management system with AES encryption for securely storing API keys and authentication tokens
- Execution history with detailed logs, performance metrics, and workflow debugging capabilities

## Use cases

- E-commerce order processing automation connecting Shopify, inventory systems, shipping providers, and customer communication tools
- Lead nurturing workflows that sync CRM data, trigger email sequences, and update marketing automation platforms based on user behavior
- IT monitoring and incident response automating Prometheus alerts, creating Jira tickets, and notifying teams via Slack or PagerDuty
- Data synchronization between business systems like syncing customer data from Salesforce to marketing tools and accounting software
- Social media management workflows that cross-post content, monitor mentions, and aggregate analytics across multiple platforms
- DevOps CI/CD pipeline integration triggering deployments, running tests, and sending notifications based on Git repository changes
- Customer support automation routing tickets, escalating issues, and updating knowledge bases based on common inquiries

## Prerequisites

- Minimum 1GB RAM (2GB+ recommended) to support n8n application, PostgreSQL database, and Redis queue processing
- Port 5678 available for n8n web interface access and API endpoints
- Basic understanding of workflow automation concepts and API integrations for creating effective n8n workflows
- Knowledge of environment variable configuration for setting up database connections and authentication credentials
- Familiarity with Docker networking concepts for troubleshooting inter-container communication issues
- Understanding of backup strategies for PostgreSQL data and n8n workflow configurations

## docker-compose.yml

```yaml
services:
  n8n:
    image: n8nio/n8n:latest
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - N8N_ENCRYPTION_KEY=${ENCRYPTION_KEY}
    volumes:
      - n8n-data:/home/node/.n8n
    ports:
      - "5678:5678"
    depends_on:
      - postgres
      - redis
    networks:
      - n8n-network
    restart: unless-stopped

  n8n-worker:
    image: n8nio/n8n:latest
    command: worker
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - N8N_ENCRYPTION_KEY=${ENCRYPTION_KEY}
    depends_on:
      - n8n
      - postgres
      - redis
    networks:
      - n8n-network
    restart: unless-stopped

  postgres:
    image: postgres:15
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=n8n
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - n8n-network
    restart: unless-stopped

  redis:
    image: redis:alpine
    volumes:
      - redis-data:/data
    networks:
      - n8n-network
    restart: unless-stopped

volumes:
  n8n-data:
  postgres-data:
  redis-data:

networks:
  n8n-network:
    driver: bridge
```

## .env

```bash
# n8n Automation
N8N_USER=admin
N8N_PASSWORD=secure_admin_password
POSTGRES_USER=n8n
POSTGRES_PASSWORD=secure_postgres_password
ENCRYPTION_KEY=your-encryption-key

# Generate key: openssl rand -hex 32
```

## Usage notes

- Web UI at http://localhost:5678
- 300+ integrations available
- Visual workflow editor
- Queue mode for scaling
- Self-hosted Zapier alternative

## Troubleshooting

- n8n interface shows 'Database connection failed': Verify POSTGRES_USER and POSTGRES_PASSWORD environment variables match between n8n and postgres containers
- Workflows fail to execute with 'Queue connection timeout': Check that Redis container is running and QUEUE_BULL_REDIS_HOST points to the correct Redis service name
- n8n-worker container crashes with 'Encryption key mismatch': Ensure N8N_ENCRYPTION_KEY environment variable is identical across n8n and n8n-worker containers
- Workflows execute slowly or time out: Increase worker container resources or add additional n8n-worker containers for better load distribution
- PostgreSQL container fails to start with 'Data directory not empty': Remove existing postgres-data volume or set POSTGRES_DB environment variable correctly
- n8n shows 'Authentication failed' on startup: Generate a strong N8N_ENCRYPTION_KEY using openssl rand -base64 32 and update environment variables

