에듀테크 프로젝트 대시보드
데이터베이스 연동 개요
FastAPI 자체는 DB 기능이 없습니다. 드라이버·(선택적) ORM·세션 관리를 직접 붙입니다.
| 드라이버 | 성격 | SQLAlchemy URL |
|---|---|---|
psycopg (psycopg3) | 동기 + 비동기 모두. 최신 권장 | postgresql+psycopg:// |
psycopg2 | 동기 전용. 레거시 다수 | postgresql+psycopg2:// (또는 postgresql://) |
asyncpg | 비동기 전용. 가장 빠름 | postgresql+asyncpg:// |
pip install "psycopg[binary]" # psycopg3
# 또는
pip install asyncpgpsycopg 직접 / databases) — 쿼리 완전 제어, 소규모postgresql+psycopg://USER:PASSWORD@HOST:PORT/DBNAME?sslmode=require
# dialect+driver :// 사용자 : 비밀번호 @ 호스트 : 포트 / DB이름 ? 옵션비밀번호에 @ : / ? 등 특수문자가 있으면 urllib.parse.quote_plus 로 인코딩.
def 라우트 (가장 단순, 버그 적음)await → 비동기(asyncpg)PostgreSQL 준비 → 동기 SQLAlchemy → 비동기 → CRUD → Alembic → 풀·트랜잭션