This repository contains the configuration files to run n8n locally with PostgreSQL and expose it securely to the internet using Cloudflare Tunnel.
This setup is detailed in the blog post: Install n8n locally using Cloudflare
The setup includes:
- n8n: Workflow automation tool
- PostgreSQL 16: Database backend for n8n
- Cloudflare Tunnel: Secure tunnel to expose n8n to the internet without opening ports
- Docker and Docker Compose
- A Cloudflare account
- A domain managed by Cloudflare
-
Clone the repository
git clone <repository-url> cd local-n8n-with-cloudflare
-
Configure environment variables
cp .env.sample .env
Edit
.envand update the following values:PUBLIC_DOMAIN: Your public domain (e.g., n8n.yourdomain.com)POSTGRES_PASSWORD: Secure password for PostgreSQL root userPOSTGRES_NON_ROOT_PASSWORD: Secure password for n8n database userN8N_ENCRYPTION_KEY: Generate usingopenssl rand -hex 32CLOUDFLARE_TUNNEL_TOKEN: Token from your Cloudflare Tunnel configurationGENERIC_TIMEZONE: Your timezone (e.g., Europe/London)
-
Start the services
docker compose up -d
-
Access n8n
Navigate to
https://your-configured-domain.comto access your n8n instance.
The setup uses three Docker containers:
- postgres: PostgreSQL database with automatic initialization of non-root user
- n8n: The n8n application configured to use PostgreSQL and HTTPS
- cloudflared: Cloudflare Tunnel daemon for secure internet access
Two Docker networks are configured:
backend: Internal network for n8n and PostgreSQL communicationcloudflare: Network for Cloudflare Tunnel to access n8n
The init-data.sh script automatically:
- Creates a non-root PostgreSQL user for n8n
- Grants appropriate permissions on the database and schema
- Sets up default privileges for future objects
Key n8n environment variables:
- Uses PostgreSQL as the database backend
- Configured for HTTPS with webhook support
- Persistent storage in Docker volumes
- Local files directory mounted at
./local-files
The tunnel is configured to route traffic from your public domain to the local n8n instance running on port 5678.
Two Docker volumes ensure data persistence:
db_storage: PostgreSQL database filesn8n_data: n8n workflows, credentials, and settings
docker compose downTo remove all data including volumes:
docker compose down -vCheck service logs:
docker compose logs -fCheck specific service:
docker compose logs -f n8n
docker compose logs -f postgres
docker compose logs -f cloudflared- Always use strong, unique passwords for database credentials
- Generate a secure encryption key for n8n
- Keep your Cloudflare Tunnel token secure and never commit it to version control
- The
.envfile is gitignored to prevent accidental exposure of secrets
See the blog post for more details: https://www.russ.cloud/2025/10/04/install-n8n-locally-using-cloudflare/