Database
Database setup, migrations, and management
Database Management
The application uses PostgreSQL as its primary database, with two main components:
- Drizzle ORM for the main application schema
- Payload CMS for content management
Schema Organization
The database is organized into multiple schemas:
public
(default): Contains the main application tables managed by Drizzle ORMpayload
: Contains Payload CMS tablesdrizzle
: Contains migration metadata
Configuration
Drizzle Configuration
The Drizzle ORM configuration is defined in drizzle.config.ts
:
Environment Variables
Required environment variables for database configuration:
Database Commands
Generate Migrations
Generate new migrations based on schema changes:
Push Schema Changes
Push schema changes directly to the database:
Sync Database
Synchronize both Drizzle and Payload schemas:
The sync process:
- Runs Drizzle migrations
- Initializes Payload CMS
- Optionally runs database seeding (if
SEED_DB=true
)
Database Studio
Launch Drizzle Studio to view and manage database:
Migrations
Migrations are stored in the src/migrations
directory and are managed by Drizzle Kit.
Migration Process
- Make changes to the schema in
src/server/db/schema.ts
- Generate migrations:
- Review generated migrations in
src/migrations
- Apply migrations:
Migration Files
- SQL migrations:
src/migrations/*.sql
- Migration metadata:
src/migrations/meta/_journal.json
Schema Management
Main Schema File
The main schema is defined in src/server/db/schema.ts
and includes:
- Table definitions
- Relations
- Indexes
- Enums
Schema Conventions
-
Use timestamps for all temporal fields:
-
Use UUIDs for primary keys:
-
Use soft deletes where appropriate:
Error Handling
The sync process includes error handling for common scenarios:
Best Practices
-
Schema Changes
- Always generate migrations for schema changes
- Review migrations before applying
- Test migrations in development first
-
Data Safety
- Use soft deletes when possible
- Add appropriate indexes for frequently queried fields
- Include proper foreign key constraints
-
Performance
- Keep migrations small and focused
- Add indexes for frequently used queries
- Use appropriate field types
-
Development Workflow
- Use
db.push
for rapid development - Use proper migrations for production changes
- Always backup database before migrations
- Use
Troubleshooting
Common Issues
-
Migration Conflicts
- Error: "relation already exists"
- Solution: Drop the conflicting objects or handle "already exists" errors
-
Schema Sync Issues
- Error: "type already exists"
- Solution: The sync process handles this automatically
-
Connection Issues
- Check DATABASE_URL format
- Verify database permissions
- Check network connectivity
Debug Commands
Database Reset
Drop All Tables
To completely reset your database and start fresh:
⚠️ WARNING: This will permanently delete all data. Use with caution and never in production.
The reset process:
- Drops all tables in both Drizzle and Payload schemas
- Removes migration history
- Re-runs all migrations
- Re-initializes Payload CMS
- Optionally runs seeds if
SEED_DB=true
Quick Reset Script
For development, you can use this one-liner to reset and resync: