Overview
Studley AI uses PostgreSQL as its primary database, with support for both Neon Database and Supabase PostgreSQL. This guide covers setup, migrations, and best practices.Database Provider Options
Supabase
Recommended for production. Includes auth, storage, and real-time features.
Neon
Serverless PostgreSQL with excellent scaling and branching.
Prerequisites
Required Extensions
Required Extensions
Your PostgreSQL database must support:
pgcrypto- For UUID generation- PostgreSQL 12+ recommended
Connection Pooling
Connection Pooling
Studley AI uses connection pooling for optimal performance:
- Max connections: 10
- Idle timeout: 30 seconds
- Connection timeout: 2 seconds
Initial Setup
Option 1: Supabase (Recommended)
Create Supabase Project
- Go to Supabase Dashboard
- Click “New Project”
- Enter project details:
- Name:
studley-ai - Database Password: (save this securely)
- Region: (choose closest to your users)
- Name:
Get Connection Details
Navigate to Project Settings > Database and copy:
- Connection String →
SUPABASE_POSTGRES_URL - Also get from Project Settings > API:
- Project URL →
NEXT_PUBLIC_SUPABASE_URL anonpublic key →NEXT_PUBLIC_SUPABASE_ANON_KEYservice_rolekey →SUPABASE_SERVICE_ROLE_KEY
- Project URL →
Option 2: Neon Database
Create Neon Project
- Go to Neon Console
- Click “Create Project”
- Choose your region and PostgreSQL version
Option 3: Local PostgreSQL
Running Migrations
Studley AI uses SQL migration scripts for database schema management.Migration Scripts Location
All migration scripts are in thescripts/ directory:
Execute Migrations
- Using psql
- Using Supabase Dashboard
- Using Node.js
Run migrations in order using
psql:Core Schema Overview
Authentication Tables
Application Tables
Verify Setup
Test your database connection:test-db.js
Backup and Maintenance
- Supabase
- Neon
- Manual Backup
Supabase provides automatic daily backups. Access them via:
- Project Settings > Database
- Scroll to “Backups”
- Download or restore as needed
Troubleshooting
Connection timeout errors
Connection timeout errors
Error:
Connection terminated unexpectedlySolutions:- Check firewall rules allow PostgreSQL port (5432)
- Verify SSL mode: add
?sslmode=requireto connection string - Increase connection timeout in
lib/db.ts
UUID generation errors
UUID generation errors
Error:
function gen_random_uuid() does not existSolution: Enable the pgcrypto extension:Migration already applied
Migration already applied
Error: Table already existsSolution: Migrations use
IF NOT EXISTS. If you see errors:- Check if migration was already run
- Review migration script for idempotency
- Skip to next migration if table exists
Permission denied errors
Permission denied errors
Error:
permission denied for schema publicSolution: Grant necessary permissions:Next Steps
Authentication Setup
Configure Supabase auth and providers
Vercel Deployment
Deploy your application to production
AI Configuration
Set up Groq AI models
Environment Variables
Review all configuration options