Skip to main content

PostgreSQL Setup

Sunschool uses PostgreSQL with advanced features including JSONB columns, array types, and partial unique indexes.

Connection Configuration

From server/db.ts, Sunschool uses Neon’s serverless driver with connection pooling:

Database URL Format

The DATABASE_URL environment variable must include:
Examples:
  • SSL required (sslmode=require)
  • WebSocket connections supported
  • Automatic connection pooling

SSL Configuration

From server/config/env.ts:
For production deployments with remote databases, always enable SSL:

Database Initialization

Create Database

  1. Log in to neon.tech
  2. Click “Create Project”
  3. Choose region (us-east-2 recommended)
  4. Copy connection string
  5. Database is ready immediately

Test Connection

Database Migrations

Auto-Migrations on Startup

From ENGINEERING.md:
Migrations run automatically on startup. Manual commands available for debugging.
The server applies pending migrations when it starts:

Manual Migration Commands

From package.json:
Run migrations manually:

Migration Files Location

From ENGINEERING.md:
Migrations are numbered and applied in order. Each migration is idempotent.

Schema Overview

From shared/schema.ts:

Core Tables

users

TypeScript schema:

learner_profiles

JSONB usage:
  • graph - Knowledge graph nodes and edges
  • subjects - Array of subject interests
  • subjectPerformance - Per-subject analytics

lessons

Special constraint: Partial unique index ensures only one ACTIVE lesson per learner:

quiz_answers

Indexes:
  • idx_learner_answers - Fast learner history queries
  • idx_question_hash - Question deduplication (SHA-256)

Gamification Tables

From ENGINEERING.md:

points_ledger

Source types:
  • QUIZ_CORRECT - Points for correct answers
  • LESSON_COMPLETE - Completion bonuses
  • ACHIEVEMENT - Achievement rewards
  • REDEMPTION - Points spent on rewards
  • ADMIN_ADJUST - Manual adjustments
  • DOUBLE_OR_LOSS_DEDUCTION - Risk mode deductions
  • GOAL_DELEGATION - Goal-based rewards

rewards

reward_redemptions

Analytics Tables

concept_mastery

Purpose: Track per-concept accuracy for adaptive learning and spaced repetition.

Schema Updates

From ENGINEERING.md:
Migrations auto-run on startup; failures don’t block server start

Creating New Migrations

Migration Safety

Always backup your database before applying migrations in production:
From the server startup logic:

Connection Pooling

From server/db.ts:
Connection limits:
  • Neon Free: 100 connections
  • Neon Pro: 1000 connections
  • Local PostgreSQL: Configurable in postgresql.conf

Health Checks

From server/auth.ts:
Test health endpoint:
Railway uses this endpoint for health monitoring (60s timeout, max 3 retries).

Troubleshooting

Connection Errors

Cause: Database server not running or wrong host/port.Fix:
Cause: Wrong username or password in DATABASE_URL.Fix:
Cause: Remote database requires SSL, but connection string missing sslmode.Fix:
Cause: Connection pool exhausted.Fix:

Migration Errors

Cause: Migration file was applied but not recorded.Fix:
Cause: Data doesn’t match new schema constraints.Fix:

Next Steps

AI Providers

Configure OpenRouter, Perplexity, and Bittensor

Security

Set up authentication and access controls

Migrations

Deep dive into database migrations

Monitoring

Set up health checks and logging