How to host n8n

To host n8n, run it on an always-on server with a public HTTPS URL, a fixed encryption key and persistent data. You can use Docker or npm.

Updated 5 min read

Learning objectives

After reading this article you will be able to:

  • Choose between Docker and npm to run n8n
  • Set up the public URL, encryption key and database
  • Check that webhooks and executions work
On this page

n8n is a workflow automation tool: you connect services (email, spreadsheets, APIs, AI models) in visual workflows. On n8n's own cloud, you just use the tool. When you self-host, you run the server and take over availability, URL, database and updates.

A workflow that starts from a webhook or a schedule only fires if the server is on all the time. That is why running n8n on your laptop is fine for testing but not for production. Check the supported versions and variable names in the official docs before you install, because they change between n8n releases.

Externalservicesends the eventPublicHTTPS URLn8nruns theworkflowwebhookwritescalls.n8n folderencryptionkeyDatabasePostgreSQLor SQLiteAI APImodel
The webhook arrives at the public HTTPS URL; n8n, always on, runs the workflow, writes to the database and calls the AI.

What do I need to host n8n?#

Four things:

  • An always-on process, so webhooks and schedules fire.
  • A public HTTPS URL, because external services must reach your webhooks.
  • Persistent data: the .n8n folder holds the encryption key and, by default, the SQLite database.
  • A fixed encryption key (N8N_ENCRYPTION_KEY) that you control.

Docker or npm?#

The n8n docs cover both. Docker uses the n8nio/n8n image, port 5678 and a volume mounted at /home/node/.n8n:

bash
docker volume create n8n_data

docker run -d --name n8n \
  -p 5678:5678 \
  -e GENERIC_TIMEZONE="America/New_York" \
  -e TZ="America/New_York" \
  -e N8N_ENCRYPTION_KEY="replace-with-a-long-random-key" \
  -v n8n_data:/home/node/.n8n \
  n8nio/n8n

The Docker page itself points to Docker Compose as the recommended install method, and the npm install is marked deprecated. If your server cannot run Docker, npm still works and needs a Node.js version within the range the docs list:

bash
npm install n8n -g
n8n start

Either way, open http://localhost:5678 to create the owner account on first run.

How do I set the public URL?#

Behind a reverse proxy, n8n listens on port 5678 internally while the public reaches it on 443. Without configuration, it builds wrong webhook URLs. According to the docs, set:

bash
N8N_HOST=n8n.yourdomain.com
N8N_PROTOCOL=https
N8N_WEBHOOK_URL=https://n8n.yourdomain.com/
N8N_PROXY_HOPS=1

N8N_WEBHOOK_URL replaces the old WEBHOOK_URL, deprecated since n8n 2.35.0. The last proxy on the path must forward the X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto headers. All of these are environment variables: keep them out of your code.

Which database should I use?#

The default is SQLite, in a database.sqlite file inside ~/.n8n. It is enough to get started, but credentials, executions and workflows live in a single file tied to the server's disk.

For production, use PostgreSQL:

bash
DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=your-host
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=your-user
DB_POSTGRESDB_PASSWORD=your-password

If the database server requires TLS with a client certificate, enable SSL and point n8n at the certificate files. Each variable accepts a _FILE suffix that reads the value from a file:

bash
DB_POSTGRESDB_SSL_ENABLED=true
DB_POSTGRESDB_SSL_CA_FILE=/path/to/ca.crt
DB_POSTGRESDB_SSL_CERT_FILE=/path/to/certificate.pem
DB_POSTGRESDB_SSL_KEY_FILE=/path/to/client.key

The n8n docs list the supported PostgreSQL versions: check that your database's version is on that list. Even with PostgreSQL, the .n8n folder still holds the encryption key and the logs, so it must be persistent too. See how to host a database and how to host a Node.js app.

Why pin N8N_ENCRYPTION_KEY?#

n8n encrypts the credentials in your workflows (API keys, passwords) with a key. If you do not set one, it generates a random key on first launch and saves it in ~/.n8n. If that disk is recreated, the key is gone and the credentials stored in the database become unreadable. Set N8N_ENCRYPTION_KEY yourself and keep a copy in a password vault. Never commit the key to a repository.

How do I check that it works?#

  1. Open the public URL: the account creation (or login) screen should load over HTTPS.
  2. Create a workflow with the Webhook node, activate it, and check that the URL shown in the editor uses your domain, not localhost.
  3. Call that URL with curl and watch the run appear in the executions list.
  4. Restart the server and confirm workflows and credentials are still there. If they vanish, the .n8n folder is not persistent.

What are the common mistakes?#

  • Webhook URL shows localhost in the editor: N8N_WEBHOOK_URL (or N8N_HOST and N8N_PROTOCOL) is missing.
  • Credentials invalid after a restart: the encryption key changed. Restore the original in N8N_ENCRYPTION_KEY.
  • Scheduled workflows fire at the wrong time: set GENERIC_TIMEZONE and TZ.
  • Data lost when recreating the container: the volume for /home/node/.n8n was not mounted.
  • Commercial use without reading the license: self-hosted n8n uses the Sustainable Use License, which has limits. Check LICENSE.md before reselling or embedding it in a product.

How to do this on Square Cloud#

n8n is a Node.js application, and on Square Cloud it runs like any API or HTTP service: in an always-on container with no sleep mode, so webhooks and schedules fire at any hour.

  1. Add n8n as a dependency in package.json. There is no build step: at start, the platform runs npm install when node_modules is missing.
  2. Use N8N_PORT=$PORT npx n8n start as the start command (START) and set N8N_LISTEN_ADDRESS=0.0.0.0, because a web application must listen on 0.0.0.0, on the port in the PORT variable.
  3. In the environment variables, set N8N_ENCRYPTION_KEY, the database variables, N8N_HOST with the app's HTTPS *.squareweb.app subdomain, N8N_PROTOCOL=https and N8N_WEBHOOK_URL.
  4. Deploy by ZIP, CLI or GitHub.

Plan RAM is a shared pool, with a minimum of 512 MB per web application. Watch n8n in the metrics: if it runs out of memory, the application stops until you raise the RAM and start it again.

The application's disk persists across restarts and deploys: point N8N_USER_FOLDER to a folder in the project and keep the .n8n folder out of the ZIP, because a commit overwrites the files it contains. For production, PostgreSQL hosting starts on Standard (from R$44.99 a month), with an automatic daily snapshot of the database. The connection requires TLS: ship the certificate you download from the dashboard with the project and point the DB_POSTGRESDB_SSL_*_FILE variables at it.

Standard also gives you the AI Gateway, an OpenAI-compatible API for the cubic model, in beta and at no extra cost during the beta. In n8n, use the HTTP Request node with POST https://api.squarecloud.app/v2/ai/chat/completions, your account API key (ai:chat scope) in the Authorization header and "model": "cubic" in the body. On that plan you get up to 2.5M tokens and up to 1,000 requests a day, one at a time; limits may change when the beta ends.

AI

Host AI apps in always-on containers

Learn more

FAQs

Is self-hosting n8n free?

You can download and run self-hosted n8n without a paid n8n license, under its Sustainable Use License, which allows internal business or non-commercial use and restricts offering n8n to outside customers. You still pay for the machine or hosting it runs on. Read the license before using it inside a commercial product.

Do I need Docker to run n8n?

No. The n8n docs describe Docker and also npm (npm install n8n -g), which needs a Node.js version within the range the docs list. The npm install is marked deprecated, so prefer Docker when you can.

Which database does n8n use?

By default, n8n uses SQLite, stored in a file inside the .n8n folder. For production, the docs support PostgreSQL through DB_TYPE=postgresdb and the DB_POSTGRESDB_* variables.

What happens if I lose N8N_ENCRYPTION_KEY?

n8n uses this key to encrypt saved credentials. Without the original key it cannot read the credentials already stored, so keep a copy outside the server.

Next articleWhatsApp AI bot: architecture and costsA WhatsApp AI bot is a webhook that receives the message from the Cloud API, calls an LLM and replies. See the architecture, the FastAPI code and what it costs.