Deploy Flask to Railway
Bulletproof template for Flask + PostgreSQL + Redis on Railway.app
Why Your Deployment Was So Hard (and How This Fixes It Forever)
The big problems you ran into:
- Railway doesn't have the
psqltool installed → raw SQL commands failed. - Manual migrations or
release:commands often don't run reliably. - Running scripts locally couldn't reach the private database.
This template solves all of that by doing the simplest, most reliable thing: Let the app create its own database tables when it starts. That one line db.create_all() in run.py is the hero.
Simple Breakdown of the Important Parts
1. run.py – The Magic File
This is where your app actually starts. The key part:
with app.app_context():
db.create_all() # ← This creates all tables automatically Every time Railway starts your app, it runs this:
- If tables don't exist → creates them.
- If they already exist → does nothing (safe!).
- No manual commands needed ever again.
2. Procfile – Tells Railway How to Run Your App
web: gunicorn run:app Just says: "Run the app defined in run.py".
Don't add release: lines → they often don't work on Railway.
3. Database Connection
Railway automatically gives your app a DATABASE_URL when you add PostgreSQL.
Your code fixes the old "postgres://" → "postgresql://" issue (SQLAlchemy needs this).
You don’t have to do anything – it just works.
4. Redis (for sessions) – Made Optional
If you add a Redis database in Railway → great, uses it for faster sessions.
If not, or if it fails → automatically falls back to saving sessions in files.
No crashes because Redis is missing.
5. Environment Variables
Only really need one in Railway:
FLASK_SECRET_KEY = some-long-random-string Generate it once with:
python -c "import secrets; print(secrets.token_hex(32))" Everything else Railway sets automatically.
6. How to Deploy Next Time (Super Simple Steps)
- Push your code to GitHub.
- In Railway: New Project → Deploy from GitHub → pick your repo.
- Add PostgreSQL (click + New → Database → PostgreSQL).
- (Optional) Add Redis the same way.
- In your app service → Variables → add
FLASK_SECRET_KEY. - Done! Railway auto-deploys.
On first deploy (or any fresh database), tables are created automatically. No extra steps.
Summary: The Golden Rules for Flask on Railway
- Always put
db.create_all()in run.py → tables auto-create. - Never rely on manual SQL files or
flask db migratefor initial setup on Railway. - Make Redis optional → avoid crashes.
- Use
web: gunicorn run:appin Procfile → keep it simple. - Let Railway set
DATABASE_URLautomatically → don't hardcode anything.
You now have a bulletproof template you can copy for every future Flask project.
This setup avoids all the common Railway deployment issues and works every time.
Copy the full template from the original document when starting a new project — you’ll deploy in minutes instead of hours. 💪
If you want to add proper migrations (Flask-Migrate/Alembic) later when the app grows, we can do that too — but for now, this setup is perfect and reliable. download RAILWAY_FLASK_TEMPLATE.md