Skip to content

lNamelessl/FastAPI-Posgres-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI + PostgreSQL Backend Template

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.


πŸš€ Quick Start

Prerequisites

  • Python 3.12+
  • PostgreSQL 14+
  • Docker & Docker Compose (optional)

⚑ Local Development (2 minutes)

# 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 --reload

Visit:

  • API: http://localhost:8000
  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

🐳 Using Docker

cp .env.example .env
docker-compose up --build

πŸ“š Documentation

  • 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

πŸ—οΈ Project Structure

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.md

πŸ” Authentication

This template uses JWT-based stateless authentication.

Flow:

  1. User logs in with email/password
  2. Backend validates credentials
  3. JWT access token is generated
  4. Frontend/client includes token in Authorization header
  5. Protected endpoints validate token automatically

Example header:

Authorization: Bearer <token>

πŸ—„οΈ Database

  • PostgreSQL
  • SQLModel
  • Alembic

Included models:

  • User
  • Item

Supports:

  • migrations
  • relationships
  • typed schemas
  • async-ready architecture

πŸ“‘ API Endpoints

Interactive docs available at:

  • Swagger UI β†’ /docs
  • ReDoc β†’ /redoc

Core endpoints:

POST /login/access-token
POST /users/
GET  /users/
GET  /items/
POST /items/
GET  /health

❀️ Health Check

Railway-ready health endpoint included:

GET /health

Returns:

{
  "status": "ok"
}

🚒 Railway Deployment

One-Click Deploy

Deploy on Railway


Manual Railway Setup

  1. Connect GitHub repository
  2. Add PostgreSQL service
  3. Configure environment variables
  4. Deploy

βš™οΈ Example railway.json

{
  "$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"
  }
}

πŸ”‘ Environment Variables

Example:

SECRET_KEY=your-secret-key
POSTGRES_SERVER=localhost
POSTGRES_PORT=5432
POSTGRES_DB=app
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password

Railway users can use:

DATABASE_URL=${{Postgres.DATABASE_URL}}

πŸ› οΈ Development

Create Migration

alembic revision --autogenerate -m "description"

Apply Migration

alembic upgrade head

Rollback Migration

alembic downgrade -1

πŸ§ͺ Testing

pytest

πŸ“¦ Key Dependencies

  • FastAPI
  • SQLModel
  • Pydantic v2
  • Alembic
  • Psycopg
  • PyJWT
  • Passlib
  • Ruff
  • Uvicorn

✨ Features

βœ… JWT Authentication βœ… PostgreSQL Integration βœ… Alembic Migrations βœ… SQLModel ORM βœ… Docker Support βœ… Railway Ready βœ… Health Checks βœ… OpenAPI Docs βœ… Clean Project Structure βœ… Production Deployment Ready


🀝 Contributing

git checkout -b feature/my-feature
git commit -m "Add feature"
git push origin feature/my-feature

Open a Pull Request.


πŸ“ License

MIT License


πŸ†˜ Troubleshooting

Database Connection Errors

  • Verify PostgreSQL is running
  • Check .env credentials
  • Confirm migrations ran successfully

Railway Deployment Issues

  • Ensure app listens on:

    0.0.0.0:$PORT
  • Verify healthcheck path:

    /health
  • Confirm environment variables are set


πŸ“§ Support

  1. Review documentation
  2. Check existing issues
  3. Open a new issue with logs/details

Happy building πŸš€

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors