Member-only story
Mastering Relational Databases in Python: From Psycopg2 to SQLAlchemy with a Real Project
4 min readMay 6, 2025
📜 Prelude: A Tale of Two Libraries
Once upon a repo in the kingdom of Python, a developer (you!) stood at the crossroads of working with PostgreSQL. Two powerful paths appeared:
- Psycopg2 — the sharp sword of raw SQL, mighty but manual.
- SQLAlchemy — a magical staff, harnessing the arcane arts of ORM.
In this tale, we will walk both paths, one after another, using the Student Enrollment System — a classic yet evergreen RDBMS example.
🏗️ Project: Student Enrollment System
We have three mighty tables:
students
: Stores each learner's details.courses
: Holds course information.enrollments
: A bridge table showing who enrolled in what.
Let’s first create this project using psycopg2, then recreate it with SQLAlchemy ORM.