Skip to content

mdhishaamakhtar/fastapi-sqlalchemy-postgres-template

Repository files navigation

FastAPI SQLAlchemy PostgreSQL Template

Minimal starter template for a CRUD API with FastAPI + SQLAlchemy + PostgreSQL.

Python FastAPI SQLAlchemy PostgreSQL Pytest

Features

  • FastAPI app with CORS enabled
  • SQLAlchemy ORM integration
  • Postgres-ready configuration via DATABASE_URL
  • CRUD endpoints for posts
  • Pytest suite (DB mocked for local test runs)
  • Docker + Docker Compose local setup

Requirements

  • Python 3.10+
  • pip
  • PostgreSQL (only needed when running app without Docker)

Local Setup (Without Docker)

  1. Create and activate venv:
python3 -m venv .venv
source .venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Add env vars:
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template
  1. Run API:
uvicorn main:app --reload

API Docs

When the server is running:

Tests

pytest

Formatting

black .

Docker (Recommended for Local Run)

  1. Create .env:
cp .env.example .env
  1. Build and start:
docker compose up --build
  1. Access:

DATABASE_URL is passed to the API container from docker-compose.yml and can be overridden via .env.

About

A simple REST API made using FastAPI and SQLAlchemy for the 2CC Session on FastAPI by DSC VIT Vellore. It has now been turned into a template and is a great place for beginners to get started!

Topics

Resources

License

Stars

Watchers

Forks

Contributors