Skip to content

qkhaipham/n8n-docker-compose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

n8n with PostgreSQL (pgvector) and Cloudflare Tunnel

A complete Docker Compose setup for running n8n locally with:

  • PostgreSQL with pgvector extension for vector database capabilities
  • Automatic database initialization with separate databases for n8n and documents
  • Cloudflare Tunnel for webhook support and external access

Features

  • 🚀 n8n workflow automation with PostgreSQL backend
  • 🔍 Vector database support using pgvector extension
  • 🌐 External webhook access via Cloudflare Tunnel
  • 📊 Dual database setup: Main n8n database + separate documents database
  • 🔧 Automated initialization with custom schema and tables

Prerequisites

  • Docker and Docker Compose installed
  • Cloudflare account with tunnel token (for webhook functionality)

Quick Start

  1. Configure environment variables

    IMPORTANT: Copy the example.env file and name it .env and adjust the variables:

    # Database Configuration
    POSTGRES_USER=your_admin_user
    POSTGRES_PASSWORD=your_secure_password
    POSTGRES_DB=n8n
    POSTGRES_DOCUMENTS_DB=documents
    
    # n8n Configuration
    N8N_HOST=your-domain.com
    N8N_PROTOCOL=https
    N8N_WEBHOOK_URL=https://your-domain.com
    
     # Get the token from the cloudflare tunnel for the N8N_HOST
    TUNNEL_TOKEN=your_cloudflare_tunnel_token
  2. Start the services

    docker-compose up -d
  3. Access n8n

  4. Stop the services

    docker-compose stop

Database Structure

The setup automatically creates two databases:

Main Database (n8n)

  • Used by n8n for storing workflows, executions, and settings
  • Configured via environment variables in n8n service

Documents Database (documents)

  • Dedicated vector database for AI/ML workflows
  • Includes pgvector extension for similarity search
  • Schema defined in postgres/sql/schema.sql
  • Perfect for RAG (Retrieval-Augmented Generation) workflows

Services

PostgreSQL (pgvector/pgvector:pg16)

  • Port: 5432
  • Extensions: pgvector for vector operations
  • Initialization: Runs postgres/init-data.sh on first startup
  • Health Check: Ensures database is ready before starting n8n

n8n (docker.n8n.io/n8nio/n8n)

  • Port: 5678
  • Database: PostgreSQL backend
  • Storage: Persistent volume for user data
  • Dependencies: Waits for PostgreSQL health check

Cloudflare Tunnel (cloudflare/cloudflared:latest)

  • Purpose: Exposes n8n to the internet for webhook functionality
  • Configuration: Uses TUNNEL_TOKEN from environment
  • Benefits: No need to open firewall ports or configure reverse proxy

Vector Database Usage

The documents database is configured with pgvector extension, making it perfect for:

  • Embeddings storage for AI workflows
  • Similarity search operations
  • RAG implementations in n8n
  • Document indexing and retrieval

Example n8n workflow usage:

  1. Store document embeddings in the documents database
  2. Perform similarity searches using pgvector functions
  3. Integrate with AI nodes for intelligent document retrieval

Customization

Database Schema

Modify postgres/sql/schema.sql to customize the documents database structure.

Initialization Script

Update postgres/init-data.sh to add custom database setup logic.

Environment Variables

All configuration is centralized in .env file for easy customization.

Troubleshooting

Database Connection Issues

# Check PostgreSQL logs
docker-compose logs postgres

# Verify database health
docker-compose exec postgres pg_isready -U admin -d n8n

Cloudflare Tunnel Issues

# Check tunnel logs
docker-compose logs cloudflared

# Verify tunnel token is valid

n8n Access Issues

# Check n8n logs
docker-compose logs n8n

# Verify n8n is healthy
curl http://localhost:5678/healthz

Data Persistence

  • Database data: Stored in Docker volume db_storage
  • n8n user data: Stored in Docker volume n8n_storage
  • Volumes persist between container restarts

About

A docker compose example to run N8N with postgres (and pgVector) and cloudflare

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published