How to deploy n8n on railway?
This guide explains how to deploy n8n on railway and gives a clear direct answer: use n8n’s official Docker image, connect it to a PostgreSQL database provided by Railway, and deploy with the Railway CLI. The steps below show commands, a Docker example, an update path, and basic security tips for beginners.
what you need
- An n8n account and access to your workflow files.
- A Railway account and a project with a PostgreSQL plugin or a connection string.
- Docker installed locally for testing.
- Railway CLI installed for deployment.
how to deploy n8n on railway – checklist
Below are numbered steps with the exact commands you can run. Follow them in order. Each command is ready to copy and paste. Replace placeholder values with your values.
Step 1: Install and login to Railway CLI
Install the Railway CLI. Then log in to your Railway account.
npm install -g @railway/cli
railway login
Step 2: Create a Railway project and add PostgreSQL
Create a new project in the Railway dashboard. Add a PostgreSQL plugin in the Railway dashboard. Copy the connection info or the DATABASE_URL from the plugin.
If you prefer to initialize from your folder, run:
railway init # create or link a project from this folder
railway link # link this folder to the Railway project
Step 3: Set PostgreSQL and n8n environment variables
Store database credentials in Railway variables. Use the values from the PostgreSQL plugin. Keep secrets out of code.
railway variables set DB_POSTGRESDB_HOST=your_host
railway variables set DB_POSTGRESDB_PORT=5432
railway variables set DB_POSTGRESDB_DATABASE=your_db
railway variables set DB_POSTGRESDB_USER=your_user
railway variables set DB_POSTGRESDB_PASSWORD=your_password
# Set basic auth for UI
railway variables set N8N_BASIC_AUTH_ACTIVE=true
railway variables set N8N_BASIC_AUTH_USER=admin
railway variables set N8N_BASIC_AUTH_PASSWORD='strong-password'
# Optional: set public URL for webhooks
railway variables set N8N_PUBLIC_API_URL=https://your-project.up.railway.app
Step 4: Dockerfile and local test
Create a simple Dockerfile that uses the official n8n image. Test locally with Docker before deployment.
# Dockerfile
FROM n8nio/n8n:latest
# Use the default entry point from the image
Run n8n locally with your PostgreSQL values to test.
docker run -d \
--name n8n \
-p 5678:5678 \
-e DB_TYPE=postgresdb \
-e DB_POSTGRESDB_HOST=your_host \
-e DB_POSTGRESDB_PORT=5432 \
-e DB_POSTGRESDB_DATABASE=your_db \
-e DB_POSTGRESDB_USER=your_user \
-e DB_POSTGRESDB_PASSWORD=your_password \
-e N8N_BASIC_AUTH_ACTIVE=true \
-e N8N_BASIC_AUTH_USER=admin \
-e N8N_BASIC_AUTH_PASSWORD='strong-password' \
n8nio/n8n:latest
Step 5: Deploy to Railway
With your project linked and variables set, deploy using Railway CLI. This will build the Dockerfile and push it to Railway.
railway up
Open the provided Railway URL in your project to access n8n. Test a webhook and a simple workflow.
Update
To update n8n, change the image tag in the Dockerfile or update the image reference. Then redeploy.
# Example: change tag in Dockerfile
FROM n8nio/n8n:latest
# or use a specific version like n8nio/n8n:0.262.0
# Redeploy
railway up
You can also pull a new image locally and test before running railway up.
Security
Keep the following security basics in mind.
- Store secrets in Railway variables, not in your repo.
- Enable N8N_BASIC_AUTH_* to protect the UI.
- Use HTTPS by using the Railway-provided domain or a custom domain with TLS.
- Limit webhook exposure by validating requests in workflows or using signatures.
- Rotate credentials and use strong passwords.
Done
You now have a working n8n deployment on Railway using Docker and PostgreSQL. Test flows, set up backups for your database, and monitor logs from the Railway dashboard.