Choorai
🎯 Cycle 4

Database Connection

Until now, data has been stored in memory. When the server restarts, the data disappears. Now let's connect PostgreSQL to persist data permanently.

What you'll learn in this Cycle

  • PostgreSQL installation and setup
  • Using SQLAlchemy ORM
  • Database migrations (Alembic)
  • Separating configuration by environment

Why Do You Need a Database?

❌ In-Memory Storage

  • Data deleted on server restart
  • Data inconsistency across multiple servers
  • Risk of running out of memory

✅ Using a Database

  • Permanent data storage
  • Same data across multiple servers
  • Efficient searching and sorting

Why PostgreSQL?

Here is why we recommend PostgreSQL among the many databases available:

  • Free & Open Source - No licensing costs
  • Proven Stability - Over 30 years of history
  • Cloud Support - Managed services available on all clouds
  • JSON Support - Flexible data storage like NoSQL

Quick Start

1. Install Dependencies

Terminal
pip install sqlalchemy psycopg2-binary alembic

2. Define Database Models

app/models/project.py
from sqlalchemy import Column, String, DateTime, Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import declarative_base
import uuid
from datetime import datetime

Base = declarative_base()

class Project(Base):
    __tablename__ = "projects"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    name = Column(String(200), nullable=False)
    description = Column(String(1000))
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

Cloud Database Options

Get started quickly with BaaS

If you want to use a database and authentication directly without a backend, check out the Supabase guide. You can access the database directly from the frontend without writing SQL.

Last updated: February 22, 2026 · Version: v0.0.1

Send Feedback

Opens a new issue page with your message.

Open GitHub Issue