fastapi-database-setup

Configure async SQLAlchemy 2.0 engine, session factory, and get_db dependency for FastAPI with PostgreSQL.

3|Updated Jan 20, 2026
One-click install
npx skills add https://github.com/agusmdev/burntop --skill fastapi-database-setup
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-database-setup
Source: https://github.com/agusmdev/burntop/tree/main/.claude/skills/fastapi-database-setup
Command: npx skills add https://github.com/agusmdev/burntop --skill fastapi-database-setup

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a clean, reusable setup for an async SQLAlchemy 2.0 engine, a session factory, and a FastAPI dependency to access the database, simplifying database integration in FastAPI apps.

Core Features & Use Cases

  • Async engine + session factory with SQLAlchemy 2.0 and PostgreSQL using asyncpg.
  • A get_db dependency to inject a database session into FastAPI routers.
  • Safe shutdown and explicit session lifecycle management to prevent leaks.

Quick Start

Create src/app/database.py and src/app/dependencies.py as described, then use get_db in your FastAPI routes with Depends(get_db).

Frequently Asked Questions about fastapi-database-setup

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up an async database connection in FastAPI with SQLAlchemy?

Set up an async database connection in FastAPI by configuring SQLAlchemy 2.0 with create_async_engine and async_sessionmaker. This creates an async engine and session factory to manage PostgreSQL connections using asyncpg.

How do I inject a database session into FastAPI routers?

Inject a database session into FastAPI routers using a get_db dependency. You apply Depends(get_db) to your route functions to provide a clean, reusable database session for each request.

Why do my async SQLAlchemy database connections leak in FastAPI?

Database connections leak without proper lifecycle management. Implementing an explicit shutdown routine and an async_sessionmaker ensures safe session disposal, preventing connection leaks during application shutdown.

Does this async database setup work with PostgreSQL and asyncpg?

Yes, this async database setup is explicitly designed for PostgreSQL using asyncpg. It configures the SQLAlchemy 2.0 async engine to leverage asyncpg for non-blocking database operations.

What is the best way to manage database session lifecycle in FastAPI?

The best way to manage database session lifecycle is using a get_db dependency combined with an explicit shutdown routine. This ensures safe session creation and disposal, preventing leaks.