AloftWorkBlog

How to self-host n8n

Last modified: 20 July, 2025

n8n installation homelab self-host vps postgres

n8n is really popular these days and everyone is using it. We do too. This guide will walk you through setting up n8n with PostgreSQL securely.

Install docker

First, you need to install docker. Docker is a really handy tool to run any software in isolation. We will run our n8n instance and a postgres database inside a docker container. Don't worry if you do not understand what it does, we just need to run a few commands.

Docker is available for all major platforms, follow the respective install guides:

Setup

Now that you have installed docker, create a file docker-compose.yml and add this code below:

services:
  n8n:
    image: n8nio/n8n
    container_name: n8n
    restart: unless-stopped
    networks:
      - n8n
    ports:
      - 5678:5678
    volumes:
      - n8n-data:/home/node/.n8n
    env_file:
      - path: .env
        required: true
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_DATABASE: n8n_db
      DB_POSTGRESDB_HOST: n8n-postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_USER: postgres
      DB_POSTGRESDB_SCHEMA: public
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
      N8N_HOST: ${N8N_HOST:-localhost} # eg, yourdomain.com or n8n.yourdomain.com
      WEBHOOK_URL: ${N8N_WEBHOOK_URL}
      N8N_METRICS: true 
      GENERIC_TIMEZONE: Asia/Kolkata
      TZ: Asia/Kolkata
    depends_on:
      - n8n-postgres

  n8n-postgres:
    image: postgres:17.5-alpine3.22
    container_name: n8n-postgres
    restart: unless-stopped
    networks:
      - n8n
    volumes:
      - n8n-postgres-data:/var/lib/postgresql/data
    env_file:
      - path: .env
        required: true
    environment:
      POSTGRES_DB: n8n_db
      POSTGRES_HOST_AUTH_METHOD: md5
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?environment variable POSTGRES_PASSWORD is not set.}

volumes:
  n8n-data:
    name: n8n-data
  n8n-postgres-data:
    name: n8n-postgres-data

networks:
  n8n:
    driver: bridge

The above configuration uses defines two services, n8n (the n8n web ui) and n8n-postgres (isolated PostgreSQL database). We are also creating two volumes name n8n-data and n8n-postgres-data. These volumes are used to store all your n8n data, including your sessions, workflows, credentials, etc. Do not delete these.

Do not worry if you do not understand these. Now, create a .env file and add these lines.

POSTGRES_PASSWORD="your-postgres-password"

# Do not change if you plan to only access it inside your domain
# Replace it to your domain or ip address if you want to access it outside of your local network
# e.g., or yourdomain.com or n8n.yourdomain.com
N8N_HOST=localhost
N8N_WEBHOOK_URL=

Replace these values with your credentials. It's recommended that you do not share your .env files.

Now you can start your application by running the following commands.

docker compose up

Navigate to http://localhost:5678. You should see the web application.

Here's two more commands, you should about.

docker compose up -d
docker compose down

The first command starts the application in detached mode. Meaning you will not see the logs message.

The second command stops your application. This does not delete your data.

Timezone

Make sure to change you the GENERIC_TIMEZONE and TZ environment variable to your local timezone. This will make it easier to debug schedules and triggers. You can find a list timezone identifiers here.

Disable command execution

If you want to disable the command execution node or the read / write to file node, just in-case you're super paranoid about security, you can disable it by pass this environment variable in your n8n service.

- NODES_EXCLUDE="[\"n8n-nodes-base.executeCommand\", \"n8n-nodes-base.readWriteFile\"]"

Conclusion

That's all you need to run n8n locally and use it across your local network. However, if you want to use n8n outside of your network or use webhooks, there's a bit more setup required. You would need to setup a reverse proxy, like nginx for that. You can also use a tunnel if you only care about webhooks.

About Us ✨

If you need someone to help setup n8n and automate tasks for your business, we can help. Book a call now.

How to self-host n8n