Overview
AI Studio uses PostgreSQL as its primary database and Drizzle ORM for type-safe database operations. This guide covers database setup, schema management, and common operations.Database Stack
- Database: PostgreSQL 15+
- ORM: Drizzle ORM 0.45+
- Hosting: Supabase (recommended) or self-hosted PostgreSQL
- Connection: Transaction pooler (port 6543) for serverless compatibility
Quick Setup
Configure database connection
Add your PostgreSQL connection string to For Supabase, use the Transaction pooler URL (port 6543).
.env.local:Push schema to database
Create all database tables from the Drizzle schema:This command reads
lib/db/schema.ts and syncs it to your database.Drizzle Configuration
AI Studio’s Drizzle configuration is defined indrizzle.config.ts:
drizzle.config.ts
dialect: Database type (PostgreSQL)schema: Location of schema definitionsout: Directory for generated migrationsdbCredentials.url: Connection string from environment
Database Client
The database client is configured inlib/db/index.ts:
lib/db/index.ts
The
prepare: false option is required when using Supabase’s Transaction pooler (port 6543) because prepared statements are not supported in transaction pooling mode.prepare: false?
- Supabase Transaction pooler doesn’t support prepared statements
- Required for serverless environments (Vercel, Netlify)
- Minimal performance impact for typical query patterns
Database Commands
AI Studio provides several npm scripts for database operations:Schema Push (Development)
- Reads schema from
lib/db/schema.ts - Applies changes directly to database
- Creates/modifies tables, columns, indexes
- No migration files generated
- Local development and prototyping
- Quick schema iterations
- Initial database setup
Generate Migrations
- Compares
lib/db/schema.tsto current database state - Generates SQL migration files in
drizzle/directory - Creates timestamped migration files
- Preserves migration history
- Before deploying to production
- When working in teams (commit migrations to Git)
- When you need rollback capability
Apply Migrations
- Runs pending migrations from
drizzle/directory - Updates database schema to latest version
- Tracks applied migrations in database
- Production deployments
- CI/CD pipelines
- Team environments with shared migrations
Drizzle Studio
- Launches web-based database GUI
- Provides visual schema explorer
- Allows data browsing and editing
- Shows table relationships
- View all tables and relationships
- Browse and filter data
- Run custom SQL queries
- Edit records inline
- Export data as JSON/CSV
Database Schema
The complete database schema is defined inlib/db/schema.ts. Here’s an overview of the main tables:
Core Tables
Relationships
Common Operations
Querying Data
Example: Fetch user projects
Inserting Data
Example: Create new project
Updating Data
Example: Update project status
Deleting Data
Example: Delete project
Migration Workflow
Development Workflow
For rapid local development:Production Workflow
For team collaboration and production deployments:Database Providers
Supabase (Recommended)
- Setup
- Benefits
- Pricing
Create project
- Go to supabase.com/dashboard
- Click “New Project”
- Choose a name, password, and region
- Wait for provisioning (~2 minutes)
Get connection string
- Go to Settings → Database
- Scroll to “Connection string”
- Select “Transaction” mode (port 6543)
- Copy the connection string
Local PostgreSQL
- macOS
- Linux
- Docker
Using Homebrew:Connection string:
Troubleshooting
Connection Errors
Error: “connect ECONNREFUSED”- Verify PostgreSQL is running
- Check port in connection string (6543 for Supabase, 5432 for local)
- Ensure no firewall blocking the connection
- Verify password in
DATABASE_URL - URL-encode special characters:
@→%40,#→%23 - For Supabase, reset database password in dashboard
- Add
{ prepare: false }to postgres client options - Verify you’re using Transaction pooler (port 6543) not Session pooler
Schema Sync Issues
Schema push fails with “relation already exists”Performance Issues
Slow queries- Add indexes to frequently queried columns
- Use
EXPLAIN ANALYZEto debug query plans - Enable connection pooling (Supabase Transaction mode)
- Increase pool size in Supabase dashboard
- Close connections properly in server actions
- Consider using Session pooler for long-running queries
Best Practices
Use migrations for production
Use migrations for production
- Always generate and commit migrations for production changes
- Test migrations on staging before production
- Keep migrations small and focused
- Never edit migration files after they’ve been applied
Type safety
Type safety
- Let Drizzle infer types from schema
- Use
typeof schema.tableName.$inferSelectfor row types - Use
typeof schema.tableName.$inferInsertfor insert types - Avoid manual type casting
Query optimization
Query optimization
- Use
withfor eager loading relations - Add indexes on foreign keys and frequently queried columns
- Use
limit()for pagination - Batch operations when possible
Schema design
Schema design
- Use
textfor IDs (nanoid) instead of auto-increment - Add
createdAtandupdatedAttimestamps - Use
notNull()for required fields - Define foreign key constraints for data integrity
Next Steps
API Reference
Learn how to use database queries in API routes and server actions
Deployment
Deploy your database and application to production