A production-ready FastAPI backend template with PostgreSQL, JWT authentication, Alembic migrations, SQLModel ORM, Docker support, and Railway deployment configuration.
Built for scalable API development with clean architecture and modern Python tooling.
- Python 3.12+
- PostgreSQL 14+
- Docker & Docker Compose (optional)
# 1. Clone repository
git clone <repo-url>
cd fastapi-postgres-template
# 2. Install dependencies
uv sync
# 3. Configure environment variables
cp .env.example .env
# Edit .env with your PostgreSQL credentials
# 4. Run database migrations
uv run alembic upgrade head
# 5. Start development server
uv run uvicorn app.main:app --reloadVisit:
- API:
http://localhost:8000 - Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
cp .env.example .env
docker-compose up --build- SETUP.md β Local development setup
- ARCHITECTURE.md β Project structure and design patterns
- DATABASE.md β Database models and migrations
- DEPLOYMENT.md β Railway deployment guide
- API.md β API documentation
fastapi-postgres-template/
βββ app/
β βββ api/ # API routes & dependencies
β βββ core/ # Settings, security, db config
β βββ models.py # SQLModel ORM models
β βββ crud.py # Database operations
β βββ schemas.py # Pydantic schemas
β βββ deps.py # Shared dependencies
β βββ main.py # FastAPI app
β
βββ alembic/ # Database migrations
β
βββ scripts/
β βββ setup-dev.sh
β βββ run-local.sh
β βββ db-reset.sh
β
βββ tests/ # Pytest test suite
β
βββ Dockerfile
βββ docker-compose.yml
βββ railway.json
βββ pyproject.toml
βββ .env.example
βββ README.mdThis template uses JWT-based stateless authentication.
Flow:
- User logs in with email/password
- Backend validates credentials
- JWT access token is generated
- Frontend/client includes token in
Authorizationheader - Protected endpoints validate token automatically
Example header:
Authorization: Bearer <token>- PostgreSQL
- SQLModel
- Alembic
Included models:
UserItem
Supports:
- migrations
- relationships
- typed schemas
- async-ready architecture
Interactive docs available at:
- Swagger UI β
/docs - ReDoc β
/redoc
Core endpoints:
POST /login/access-token
POST /users/
GET /users/
GET /items/
POST /items/
GET /healthRailway-ready health endpoint included:
GET /healthReturns:
{
"status": "ok"
}- Connect GitHub repository
- Add PostgreSQL service
- Configure environment variables
- Deploy
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "uvicorn app.main:app --host 0.0.0.0 --port $PORT",
"healthcheckPath": "/health",
"healthcheckTimeout": 100,
"restartPolicyType": "ON_FAILURE"
}
}Example:
SECRET_KEY=your-secret-key
POSTGRES_SERVER=localhost
POSTGRES_PORT=5432
POSTGRES_DB=app
POSTGRES_USER=postgres
POSTGRES_PASSWORD=passwordRailway users can use:
DATABASE_URL=${{Postgres.DATABASE_URL}}alembic revision --autogenerate -m "description"alembic upgrade headalembic downgrade -1pytest- FastAPI
- SQLModel
- Pydantic v2
- Alembic
- Psycopg
- PyJWT
- Passlib
- Ruff
- Uvicorn
β JWT Authentication β PostgreSQL Integration β Alembic Migrations β SQLModel ORM β Docker Support β Railway Ready β Health Checks β OpenAPI Docs β Clean Project Structure β Production Deployment Ready
git checkout -b feature/my-feature
git commit -m "Add feature"
git push origin feature/my-featureOpen a Pull Request.
MIT License
- Verify PostgreSQL is running
- Check
.envcredentials - Confirm migrations ran successfully
-
Ensure app listens on:
0.0.0.0:$PORT -
Verify healthcheck path:
/health
-
Confirm environment variables are set
- Review documentation
- Check existing issues
- Open a new issue with logs/details
Happy building π