Skip to content

Instantly share code, notes, and snippets.

@GiladShoham
Last active May 11, 2025 20:48
Show Gist options
  • Select an option

  • Save GiladShoham/eaa63148e29ce8ed1859f06dbb245b50 to your computer and use it in GitHub Desktop.

Select an option

Save GiladShoham/eaa63148e29ce8ed1859f06dbb245b50 to your computer and use it in GitHub Desktop.
n8n + postgres + cloudflare tunnel - compose
POSTGRES_USER=<user>
POSTGRES_PASSWORD=<pass>
POSTGRES_DB=n8n
POSTGRES_NON_ROOT_USER=n8nuser
POSTGRES_NON_ROOT_PASSWORD=<password>
# DOMAIN_NAME and SUBDOMAIN together determine where n8n will be reachable from
# The top level domain to serve from
DOMAIN_NAME=my-domain.com
# The subdomain to serve from
SUBDOMAIN=n8n
CLOUDFLARE_TUNNEL_TOKEN=<token>
services:
cloudflare-tunnel:
image: cloudflare/cloudflared
container_name: cloudflare-tunnel
hostname: cloudflare-tunnel
restart: unless-stopped
network_mode: "host"
command: tunnel run
volumes:
- /etc/localtime:/etc/localtime:ro
- ./config/hosts:/etc/hosts
environment:
- "TUNNEL_TOKEN=${CLOUDFLARE_TUNNEL_TOKEN}"
# volumes:
# db_storage:
# n8n_storage:
networks:
shared-postgres-net:
external: true
services:
n8n:
image: docker.n8n.io/n8nio/n8n
restart: always
networks:
- shared-postgres-net
environment:
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- N8N_RUNNERS_ENABLED=true
- WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
- N8N_SECURE_COOKIE=false
ports:
- 5678:5678
# links:
# - postgres
volumes:
- ./n8n_storage:/home/node/.n8n
# depends_on:
# postgres:
# condition: service_healthy
# volumes:
# db_storage:
# n8n_storage:
networks:
shared-postgres-net:
external: true
services:
postgres:
image: postgres:16
restart: always
networks:
shared-postgres-net:
aliases:
- postgres
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
- POSTGRES_NON_ROOT_USER
- POSTGRES_NON_ROOT_PASSWORD
volumes:
- ./db_storage:/var/lib/postgresql/data
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
- ./init-npm-db.sh:/docker-entrypoint-initdb.d/init-npm-db.sh
healthcheck:
test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
interval: 5s
timeout: 5s
retries: 10
#!/bin/bash
set -e;
if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
EOSQL
else
echo "SETUP INFO: No Environment variables given!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment