PostgreSQL Setup
Sunschool uses PostgreSQL with advanced features including JSONB columns, array types, and partial unique indexes.Connection Configuration
Fromserver/db.ts, Sunschool uses Neon’s serverless driver with connection pooling:
Database URL Format
TheDATABASE_URL environment variable must include:
- Neon Serverless
- Local PostgreSQL
- Railway PostgreSQL
- SSL required (
sslmode=require) - WebSocket connections supported
- Automatic connection pooling
SSL Configuration
Fromserver/config/env.ts:
Database Initialization
Create Database
- Neon Dashboard
- Local PostgreSQL
- Docker
- Log in to neon.tech
- Click “Create Project”
- Choose region (us-east-2 recommended)
- Copy connection string
- Database is ready immediately
Test Connection
Database Migrations
Auto-Migrations on Startup
FromENGINEERING.md:
Migrations run automatically on startup. Manual commands available for debugging.The server applies pending migrations when it starts:
Manual Migration Commands
Frompackage.json:
Migration Files Location
FromENGINEERING.md:
Schema Overview
Fromshared/schema.ts:
Core Tables
users
learner_profiles
graph- Knowledge graph nodes and edgessubjects- Array of subject interestssubjectPerformance- Per-subject analytics
lessons
quiz_answers
idx_learner_answers- Fast learner history queriesidx_question_hash- Question deduplication (SHA-256)
Gamification Tables
FromENGINEERING.md:
points_ledger
QUIZ_CORRECT- Points for correct answersLESSON_COMPLETE- Completion bonusesACHIEVEMENT- Achievement rewardsREDEMPTION- Points spent on rewardsADMIN_ADJUST- Manual adjustmentsDOUBLE_OR_LOSS_DEDUCTION- Risk mode deductionsGOAL_DELEGATION- Goal-based rewards
rewards
reward_redemptions
Analytics Tables
concept_mastery
Schema Updates
FromENGINEERING.md:
Migrations auto-run on startup; failures don’t block server start
Creating New Migrations
Migration Safety
From the server startup logic:Connection Pooling
Fromserver/db.ts:
- Neon Free: 100 connections
- Neon Pro: 1000 connections
- Local PostgreSQL: Configurable in
postgresql.conf
Health Checks
Fromserver/auth.ts:
Troubleshooting
Connection Errors
ECONNREFUSED
ECONNREFUSED
Cause: Database server not running or wrong host/port.Fix:
Authentication failed
Authentication failed
Cause: Wrong username or password in DATABASE_URL.Fix:
SSL required
SSL required
Cause: Remote database requires SSL, but connection string missing
sslmode.Fix:Too many connections
Too many connections
Cause: Connection pool exhausted.Fix:
Migration Errors
Migration already applied
Migration already applied
Cause: Migration file was applied but not recorded.Fix:
Constraint violation
Constraint violation
Cause: Data doesn’t match new schema constraints.Fix: