Published on

Setting Up n8n on Railway with External Database

Authors

n8n (pronounced "n-eight-n") is an open-source alternative to workflow automation tools like Zapier of IFTTT where you can connect multiple APIs, workflows together based and create your workflow based on your requirements. And stitch things together with or without code based on your preference.

We'll be using Railway.app to host of n8n instance for better deployment workflow. They've lots of features like one-click deployment, docker support, preview environments, usage-based pricing, etc

They also provide, $5 credits per month to try out their service (even after becoming a paid customer)

With Railway, we can focus on our REAL problem+solution rather than managing infrastructure.

And one more things, due to security concerns I prefer not to host both the app (which has an encryption key) and database (which has credentials used in the workflow) in a single platform.

Since in the event the service gets hacked, the impact will be very huge.

So we'll be using a different Postgres database. I prefer using Supabase. You can use other providers as well.

So let's get started:

Create a new repo or fork this Railway template for n8n

FROM n8nio/n8n

ARG PGPASSWORD
ARG PGHOST
ARG PGPORT
ARG PGDATABASE
ARG PGUSER

ARG USERNAME
ARG PASSWORD

ARG N8N_ENCRYPTION_KEY
ARG WEBHOOK_URL
ARG TZ

ENV DB_TYPE=postgresdb
ENV DB_POSTGRESDB_DATABASE=$PGDATABASE
ENV DB_POSTGRESDB_HOST=$PGHOST
ENV DB_POSTGRESDB_PORT=$PGPORT
ENV DB_POSTGRESDB_USER=$PGUSER
ENV DB_POSTGRESDB_PASSWORD=$PGPASSWORD

ENV N8N_BASIC_AUTH_ACTIVE=true
ENV N8N_BASIC_AUTH_USER=$USERNAME
ENV N8N_BASIC_AUTH_PASSWORD=$PASSWORD

# This key is used to encrypt your creds in the database. 
# If it's not given it'll generate a new one during each build and we can't access existing creds in the database
ENV N8N_ENCRYPTION_KEY=$N8N_ENCRYPTION_KEY
# Change this to your timezone
ENV TZ=Asia/Calcutta 
# Your webhook/n8n URL. Eg: https://n8n.example.com
ENV WEBHOOK_URL=$WEBHOOK_URL

CMD ["n8n", "start"]

And now, just set up your project in Railway, configure the env props (especially, the database creds)

And if you plan on using Supabase, you can get the DB credentials from their dashboard and configure it in the railway app.

Happy slick workflows