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
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.
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
.n8nfolder 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:
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/n8nThe 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:
npm install n8n -g
n8n startEither 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:
N8N_HOST=n8n.yourdomain.com
N8N_PROTOCOL=https
N8N_WEBHOOK_URL=https://n8n.yourdomain.com/
N8N_PROXY_HOPS=1N8N_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:
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-passwordIf 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:
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.keyThe 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?#
- Open the public URL: the account creation (or login) screen should load over HTTPS.
- Create a workflow with the Webhook node, activate it, and check that the URL shown in the editor uses your domain, not
localhost. - Call that URL with
curland watch the run appear in the executions list. - Restart the server and confirm workflows and credentials are still there. If they vanish, the
.n8nfolder is not persistent.
What are the common mistakes?#
- Webhook URL shows
localhostin the editor:N8N_WEBHOOK_URL(orN8N_HOSTandN8N_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_TIMEZONEandTZ. - Data lost when recreating the container: the volume for
/home/node/.n8nwas 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.
- Add
n8nas a dependency inpackage.json. There is no build step: at start, the platform runsnpm installwhennode_modulesis missing. - Use
N8N_PORT=$PORT npx n8n startas the start command (START) and setN8N_LISTEN_ADDRESS=0.0.0.0, because a web application must listen on0.0.0.0, on the port in thePORTvariable. - In the environment variables, set
N8N_ENCRYPTION_KEY, the database variables,N8N_HOSTwith the app's HTTPS*.squareweb.appsubdomain,N8N_PROTOCOL=httpsandN8N_WEBHOOK_URL. - 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
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.
More in AI
See all 13 articles- How much does an AI chatbot cost to build and run?Running an AI chatbot costs model tokens, hosting, a database and, with RAG, extra context per message. See how to estimate the cost per conversation.
- How much does a RAG system cost? Embeddings, database and LLMA RAG system has three costs: embeddings, a vector database and the LLM. See the math for 1,000 questions a day, with hypothetical prices, and where the bill grows.
- How to build a Discord AI botBuild a Discord AI bot with discord.js: an /ask command, the API key in an environment variable, per-channel memory, Discord limits and how to host it.
- How to host an AI chatbotAn AI chatbot is an API that takes messages, calls a language model and streams the reply back. Here is the code and how to put it online.