Python FastAPI Tutorial (Part 15): PostgreSQL and Alembic - Database Migrations for Production

Corey Schafer
Corey SchaferApr 3, 2026

Why It Matters

Switching to PostgreSQL and Alembic gives FastAPI applications production‑grade reliability, enabling safe schema changes and robust concurrent access without risking data loss.

Key Takeaways

  • Replace SQLite with PostgreSQL for production‑grade concurrency handling.
  • Use Alembic for version‑controlled database migrations in FastAPI.
  • Store DB URL in Pydantic settings, avoiding hard‑coded credentials.
  • Remove create_all() and rely on migration scripts for schema changes.
  • Install async‑compatible psycopg driver and configure async SQLAlchemy engine.

Summary

The video walks developers through converting a FastAPI tutorial project from a development‑only SQLite database to a production‑ready PostgreSQL setup, and introduces Alembic for managing schema migrations. It explains why SQLite’s file‑based model and the use of create_all() are unsuitable for production, then shows how to install PostgreSQL locally (via Homebrew, apt, or Docker), create a dedicated user and database, and verify connectivity with psql.

Key technical steps include installing the async‑compatible psycopg driver, moving the database URL out of hard‑coded code into a Pydantic Settings class, and loading it from an .env file. The tutorial removes the create_all() call from the app lifespan, cleans up unused imports, and configures SQLAlchemy’s async engine to use the new URL. Alembic is added via uv, initialized with the async template, and its directory structure (alembic.ini, env.py, versions folder) is explained as the foundation for version‑controlled migrations.

The presenter highlights practical examples: a psql command sequence to create a user (blog_user) and database (blog), the “if not exists” limitation of create_all(), and the analogy that each Alembic migration file acts like a Git commit for the database schema. He also stresses best practices such as storing credentials in .env and adding the file to .gitignore to avoid accidental exposure.

By adopting PostgreSQL and Alembic, developers gain reliable concurrent write handling, safe incremental schema evolution, and a workflow that mirrors production environments. This transition eliminates the risk of data loss from table recreation and aligns the project with industry‑standard tooling for scalable FastAPI deployments.

Original Description

In this video, we'll be making our database setup production-ready by moving from SQLite to PostgreSQL and introducing Alembic for database migrations. We'll cover how to install and configure PostgreSQL locally, how to replace SQLite and create_all with a proper migration workflow, how to set up Alembic for managing schema changes, and how to generate and apply migrations so that we never have to delete and recreate our database again. By the end of this video, you'll have a solid workflow for updating your database structure as your application evolves. Let's get started...
The code from this video can be found here:
Full FastAPI Course:
PostgreSQL Installation - https://www.postgresql.org/download/
✅ Support My Channel Through Patreon:
✅ Become a Channel Member:
✅ One-Time Contribution Through PayPal:
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
✅ Equipment I Use and Books I Recommend:
▶️ You Can Find Me On:
My Website - http://coreyms.com/
#Python #FastAPI

Comments

Want to join the conversation?

Loading comments...