Database

PostgreSQL with SQLAlchemy ORM and Alembic migrations.

Database Setup

ForgeAPI uses PostgreSQL as its primary database with Redis for caching and session management.

ComponentPurposeDefault Port
PostgreSQLPrimary data storage5432
RedisCaching & sessions6379

Models

app/models/user.py
from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.sql import func
from app.db.base import Base

class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True, nullable=False)
    hashed_password = Column(String, nullable=False)
    is_active = Column(Boolean, default=True)
    is_verified = Column(Boolean, default=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

Migrations

Database migrations are handled by Alembic:

Terminal
# Create a new migration
alembic revision --autogenerate -m "Add user table"

# Apply migrations
alembic upgrade head

# Rollback one migration
alembic downgrade -1

Production Migrations

Always backup your database before running migrations in production. Test migrations in a staging environment first.