01Why Self-Host Your Automation?
02Docker Compose Setup with PostgreSQL
1services: 2 n8n: 3 image: n8nio/n8n:1.824 environment: 5 - N8N_HOST=n8n.example.com6 - N8N_PORT=56787 - N8N_PROTOCOL=https8 - WEBHOOK_URL=https://n8n.example.com/9 - DB_TYPE=postgresdb10 - DB_POSTGRESDB_HOST=postgres11 - DB_POSTGRESDB_DATABASE=n8n12 - DB_POSTGRESDB_USER=n8n13 - DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}14 - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}15 - GENERIC_TIMEZONE=America/New_York16 volumes: 17 - n8n-data:/home/node/.n8n18 ports: 19 - "5678:5678"20 depends_on: 21 postgres: 22 condition: service_healthy23 restart: unless-stopped2425 postgres: 26 image: postgres:16-alpine27 environment: 28 - POSTGRES_DB=n8n29 - POSTGRES_USER=n8n30 - POSTGRES_PASSWORD=${DB_PASSWORD}31 volumes: 32 - postgres-data:/var/lib/postgresql/data33 healthcheck: 34 test: ["CMD", "pg_isready", "-U", "n8n"]35 interval: 10s36 timeout: 5s37 retries: 538 restart: unless-stopped3940volumes: 41 n8n-data: 42 postgres-data: The N8N_ENCRYPTION_KEY is used to encrypt credentials stored in the database. Generate it once with openssl rand -hex 32 and save it securely. If you lose this key, all stored credentials become unreadable and you'll need to re-enter them. Back it up alongside your database.
03Building Your First Workflow
1# Export all workflows for backup2docker compose exec n8n n8n export:workflow --all --output=/home/node/.n8n/backups/34# Import a workflow from JSON5docker compose exec n8n n8n import:workflow --input=/home/node/.n8n/workflows/my-workflow.json67# Export credentials (encrypted)8docker compose exec n8n n8n export:credentials --all --output=/home/node/.n8n/backups/910# List all workflows11docker compose exec n8n n8n list:workflow04Connecting Your Self-Hosted Stack
1services: 2 n8n: 3 image: n8nio/n8n:1.824 networks: 5 - n8n-net6 - monitoring-net # Access Grafana, Prometheus7 - app-net # Access your application stack8 # ... rest of config910networks: 11 n8n-net: 12 monitoring-net: 13 external: true # Created by monitoring stack14 app-net: 15 external: true # Created by app stackWhen connecting n8n to internal services, use dedicated API keys with minimal permissions. n8n stores credentials encrypted in its database, but if your n8n instance is compromised, those credentials provide access to every connected service. Apply least-privilege: read-only API keys where possible, scoped tokens instead of admin credentials.