# n8n Workflow Automation

> n8n workflow automation platform with PostgreSQL for workflow data persistence.

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

## Overview

n8n is a fair-code workflow automation platform that emerged as a powerful self-hosted alternative to services like Zapier and Microsoft Power Automate. Built with a visual workflow designer, n8n enables users to create complex automation sequences connecting over 350 different services and applications without requiring extensive coding knowledge. The platform supports custom JavaScript nodes, webhook triggers, and advanced error handling, making it suitable for both simple task automation and sophisticated business process workflows.

This stack combines n8n with PostgreSQL to create a production-ready automation platform with persistent data storage. While n8n can operate with SQLite for development, PostgreSQL provides the ACID compliance, concurrent user support, and data integrity required for business-critical workflows. The PostgreSQL backend stores workflow definitions, execution history, credentials, and user data, ensuring that complex automation sequences remain reliable and auditable over time.

This configuration targets organizations and teams seeking workflow automation independence without recurring SaaS fees. DevOps teams can deploy internal automation workflows, marketing departments can orchestrate multi-channel campaigns, and data engineers can build ETL pipelines with visual interfaces. The self-hosted approach provides complete control over sensitive credentials and workflow logic while offering the scalability to handle thousands of workflow executions.

## Features

- Visual workflow builder with drag-and-drop interface for creating complex automation sequences
- 350+ pre-built integrations including popular services like Slack, GitHub, Google Sheets, and AWS
- Custom JavaScript code nodes for advanced data transformation and business logic
- Webhook endpoints for triggering workflows from external applications and services
- Scheduled workflow execution with cron-like syntax and timezone support
- Comprehensive execution history with detailed logs stored in PostgreSQL
- Credential management system with encrypted storage and team sharing capabilities
- Error handling and retry mechanisms with exponential backoff and custom error workflows

## Use cases

- DevOps automation for deployment notifications, incident response, and infrastructure monitoring alerts
- Marketing automation workflows connecting CRM systems, email platforms, and social media management tools
- Data synchronization between business applications like syncing leads from web forms to CRM and project management tools
- E-commerce automation for order processing, inventory management, and customer communication workflows
- Content management workflows for publishing blog posts, social media updates, and documentation across multiple platforms
- IT service management automation for ticket routing, user provisioning, and system health monitoring
- Financial data processing including invoice generation, expense tracking, and automated reporting workflows

## Prerequisites

- Docker and Docker Compose installed with support for health checks and depends_on conditions
- Minimum 768MB RAM available (256MB for PostgreSQL, 512MB for n8n workflow execution)
- Port 5678 available for n8n web interface access
- Basic understanding of workflow concepts and API integrations for building automation sequences
- Environment variable configuration for database credentials and authentication settings
- SSL/TLS certificate if exposing n8n to external webhook triggers over HTTPS

## docker-compose.yml

```yaml
services:
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    environment:
      - N8N_HOST=${N8N_HOST}
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - NODE_ENV=production
      - WEBHOOK_URL=${WEBHOOK_URL}
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - 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}
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_BASIC_AUTH_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_BASIC_AUTH_PASSWORD}
    ports:
      - "5678:5678"
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - n8n-network

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

volumes:
  n8n_data:
  postgres_data:

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

## .env

```bash
# n8n Workflow Automation
N8N_HOST=localhost
WEBHOOK_URL=http://localhost:5678
GENERIC_TIMEZONE=UTC
POSTGRES_USER=n8n
POSTGRES_PASSWORD=n8n_password
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=changeme
```

## Usage notes

- Editor at http://localhost:5678
- 400+ integrations available
- Self-hosted Zapier alternative
- Supports webhooks and schedules
- Create custom nodes with JavaScript

## Troubleshooting

- n8n container fails to start with database connection error: Verify PostgreSQL container is healthy and environment variables DB_POSTGRESDB_USER and DB_POSTGRESDB_PASSWORD match POSTGRES_USER and POSTGRES_PASSWORD
- Workflow executions fail with 'Workflow could not be started' error: Check n8n container logs for memory issues and increase Docker memory allocation if workflows involve large data processing
- Webhooks return 404 or timeout errors: Ensure WEBHOOK_URL environment variable matches your external domain and that firewall rules allow inbound connections to port 5678
- PostgreSQL container exits with permission denied errors: Verify the postgres_data volume has correct ownership or delete the volume to reinitialize with proper permissions
- n8n interface shows 'Cannot connect to database' after restart: Wait for PostgreSQL health check to pass or check if database initialization completed by examining postgres container logs
- Custom nodes fail to load with JavaScript execution errors: Verify NODE_ENV is set to production and check that custom node dependencies are properly installed in the n8n container

