Introduction
Railway is a popular platform that makes it easy to deploy web apps and databases quickly. In this guide, you will learn how to deploy a Django application that uses PostgreSQL on Railway. The instructions are written in simple, natural language and include all required steps, commands, and configuration examples.
Overview of the Steps
Prepare your Django project for production.
Add PostgreSQL dependency and database config.
Configure static files and WhiteNoise.
Create a
requirements.txt,Procfile, andruntime.txtPush your code to GitHub (or connect a repo)
Create a Railway project and add a PostgreSQL plugin.
Set environment variables on Railway (DATABASE_URL, SECRET_KEY, DEBUG, ALLOWED_HOSTS)
Deploy and run migrations.
Collect static files and create a superuser.
Troubleshoot and monitor logs
1. Prepare Your Django Project for Production
Ensure your Django project is functioning locally and utilizes a virtual environment. Install production-ready components:
python -m venv .venv
source .venv/bin/activate # macOS / Linux
.\.venv\Scripts\activate # Windows
pip install django gunicorn psycopg2-binary dj-database-url python-dotenv whitenoise
Key packages explained:
gunicorn— WSGI HTTP server for running Django in production.psycopg2-binary— PostgreSQL adapter for Python.dj-database-url— Parse DATABASE_URL into DjangoDATABASESconfig.python-dotenv— Load.envlocally (optional).whitenoise— Serve static files efficiently without a separate CDN during initial setup.
2. Add PostgreSQL Dependency and Database Config
In settings.py, replace the default SQLite configuration with code that reads DATABASE_URL:
# settings.py
import os
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default='sqlite:///db.sqlite3')
}
This allows Railway to provide a DATABASE_URL environment variable that Django will use automatically.
3. Configure Static Files with WhiteNoise
Configure static files so that Django can serve them in production.
Install whitenoise (already included above) and update settings.py:
# settings.py (near top)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ... other middleware
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Optional: compress and cache static files
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
This config reduces latency and removes the need for a separate static server for small/medium apps.
4. Create requirements.txt, Procfile, and runtime.txt
Generate requirements.txt:
pip freeze > requirements.txt
Create a Procfile (root of repo):
web: gunicorn yourprojectname.wsgi --log-file -
Create runtime.txt to pin Python version (optional but recommended):
python-3.11.6
Replace yourprojectname with the name of the Django project folder containing wsgi.py.
5. Use Environment Variables Locally (Optional)
Create a .env file for local testing (DO NOT commit .env to git):
SECRET_KEY=your_local_secret_key
DEBUG=True
DATABASE_URL=postgres://user:pass@localhost:5432/dbname
Load it locally with python-dotenv or your chosen method.
6. Push Your Code to GitHub
Railway connects to GitHub repositories. Initialize git, commit, and push:
git init
git add .
git commit -m "Prepare app for Railway deployment"
git branch -M main
git remote add origin https://github.com/your-username/your-repo.git
git push -u origin main
7. Create a Railway Project and Add PostgreSQL
Sign in to Railway.app (or create an account).
Click New Project → Deploy from GitHub repo and connect your repository.
After creating the project, add a PostgreSQL plugin:
In the Railway dashboard, click Add Plugin → Postgres.
Railway provisions the database and provides a
DATABASE_URLvariable.
Railway will also show project secrets under the Variables (environment) section.
8. Set Environment Variables on Railway
In the Railway project settings or environment variables UI, add the following variables:
DATABASE_URL— provided automatically by the Postgres pluginSECRET_KEY— a secure secret for DjangoDEBUG— set toFalsein productionALLOWED_HOSTS— set to your Railway domain (e.g.,your-project.up.railway.app) or*for testing
Join the conversation! Your thoughts help the community grow.