Filipe E.

Running n8n locally

Most n8n tutorials tell you to throw your automations on a random VPS. That VPS then holds the keys to your inbox, driver, social media and sometimes even your finances. This is how I run n8n locally in a way I am comfortable with and capable of running heavy workloads.

Why local?

Two main reasons: security and cost. The last thing you want is some server, somewhere, with full access to your mailbox, google account, social media, all the files on your laptop and so on. Maybe you go further and you want to give it access to your finances and taxes. Sky is the limit, right? On top of this, a VPS or a n8n instance hosted in the cloud will cost you money which is completely unnecessary given that n8n provides a community edition for free, which you can install on your computer, comes with unlimited workflows and is more than sufficient to get started and build your automations.

Data in. Data out.

Running it locally does not give you any guarantee that your data is 100% safe; however, it ensures that any data you need to feed into n8n (data in) as part of its workflow execution(s) will live within your hardware and network.

Data that you send (data out) to external services (e.g., a cloud-based LLM) is, for the most part, beyond your control in terms of security, and should always be handled with extreme care to minimize privacy and exposure risks.

You must keep this in mind every time you share or give access to your data.

docker diagram

What is the overall setup?

Essentially, you'll be running 4 containers:

Docker Compose (compose.yaml)

services:
  postgres:
    image: postgres:16
    container_name: n8n_postgres_db
    restart: unless-stopped
    environment:
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_NON_ROOT_USER
      - POSTGRES_NON_ROOT_PASSWORD
    volumes:
      - n8n_db:/var/lib/postgresql/data
      - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
      interval: 5s
      timeout: 5s
      retries: 10
  n8n-redis:
    image: redis:7-alpine
    container_name: n8n-redis
    restart: always
    volumes:
      - redis-data:/data
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    environment:
      - GENERIC_TIMEZONE=Europe/Amsterdam
      - NODE_ENV=production
      - N8N_SECURE_COOKIE=false
      - N8N_RUNNERS_ENABLED=true
      - N8N_RUNNERS_MODE=external
      - N8N_RUNNERS_AUTH_TOKEN
      - N8N_RUNNERS_BROKER_PORT=5679
      - N8N_EDITOR_BASE_URL=http://localhost
      - WEBHOOK_URL=http://localhost
      - N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
      - OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS=true
        # Postgres DB configuration
      - 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}
        # Queue mode configuration
      - QUEUE_MODE=redis
      - QUEUE_BULL_REDIS_HOST=n8n-redis
      - QUEUE_BULL_REDIS_PORT=6379
      - QUEUE_BULL_REDIS_DB=1
    ports:
      - "5678:5678"
      - "5679:5679"
    links:
      - postgres
    volumes:
      - n8n_data:/home/node/.n8n
    restart: unless-stopped
    depends_on:
      - postgres
      - n8n-redis
  task-runners:
    image: n8nio/runners:latest
    container_name: n8n-runners
    environment:
      - N8N_RUNNERS_AUTH_TOKEN
      - N8N_RUNNERS_BROKER_HOST=n8n
      - N8N_RUNNERS_BROKER_PORT=5679
      - N8N_RUNNERS_MAX_CONCURRENCY=5
      - N8N_RUNNERS_TASK_TIMEOUT=60
      - NODE_OPTIONS=--max-old-space-size=2048
      - N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679
      - N8N_RUNNERS_MODE=external
      - N8N_RUNNERS_ENABLED_TASK_TYPES=javascript,python
      - N8N_RUNNERS_STDLIB_ALLOW=*
    depends_on:
      - postgres
      - n8n
      - n8n-redis
volumes:
  n8n_db:
  n8n_data:
    name: n8n_data
  redis-data:

The postgres container references a init-data.sh file. This file is required to bootstrap the database with the runtime user for n8n.

#!/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

Make sure the file is executable:

chmod +x init-data.sh

Finally, you might have noticed there are no usernames, passwords or tokens within the Docker compose file. This is because I chose to work with a .env file to keep all of those secrets.

Here's a sample .env file:

POSTGRES_USER=n8n_user
POSTGRES_PASSWORD=SOMEPASSWORD.12345
POSTGRES_DB=n8n_database
POSTGRES_NON_ROOT_USER=n8n_appuser
POSTGRES_NON_ROOT_PASSWORD=SOMEOTHERPASSWORD.67890
N8N_RUNNERS_AUTH_TOKEN=12345example2026

What to know and what to change

.env file

Compose.yaml

Running n8n

To launch your n8n use:
docker compose up -d

It will take a few seconds to pull and build the containers, but as soon as it's ready, you can head to the URL you specified above to check your n8n instance.

Additional considerations

Public URL + HTTPS

I highly recommend you do this.
Sooner or later, you will realize that you need to expose your n8n instance to the outside with a stable public URL and over HTTPS in order to integrate with external services (Google OAuth is one of them). I find it the easiest to use a service like ngrok or zrok. They both have their limitations, but for the most part, it does what you need with their free tier.
Important: If you do this, I recommend changing the N8N_SECURE_COOKIE variable on the Compose.yaml to true.

Strict inbound traffic.

This one depends on how much you know about networks and firewalls. But if you are keen on keeping your instance secure, you could think of setting up your firewall so that it does not allow any inbound connections unless they come from specific IPs. This can be tricky when using cloud services although many providers provider a list of IPs, or CIDR blocks, that you can use. Example.

Housekeeping Tips

Keeping you n8n setup up to date

There is a new release almost every week, so keeping n8n up to date ensures you always get the latest features and fixes as soon as possible. Here's how you can update your environment:

docker compose pull
docker compose up -d

This will update the containers where new images are available and bring them back online.

All files available on Github.