Database Stack
- Database: PostgreSQL 16.4 (Alpine Linux)
- ORM: Drizzle ORM
- Migration Tool: Drizzle Kit
- Client Library:
postgres(node-postgres)
Quick Start
The fastest way to get your database up and running:Database Setup Options
Thepnpm db:setup script offers two options:
Option 1: Local PostgreSQL (Docker)
Recommended for development. Automatically sets up a PostgreSQL container. Requirements:- Docker installed and running
- Creates a
docker-compose.ymlfile - Starts a PostgreSQL 16.4 container
- Configures the database with default credentials
- Sets
POSTGRES_URLtopostgres://postgres:postgres@localhost:54322/postgres
docker-compose.yml
The PostgreSQL container runs on port 54322 (not the default 5432) to avoid conflicts with other PostgreSQL installations.
Option 2: Remote PostgreSQL
Use a managed PostgreSQL service for development or production. Recommended Providers: When prompted bypnpm db:setup, choose option R and enter your connection string.
Drizzle Configuration
The database configuration is defined indrizzle.config.ts:
drizzle.config.ts
Path to your database schema definition (
lib/db/schema.ts)Directory where migration files are generated (
lib/db/migrations)Database dialect -
postgresql in this casePostgreSQL connection string from the
POSTGRES_URL environment variableDatabase Schema
The starter includes the following tables (defined inlib/db/schema.ts):
Users Table
Stores user accounts and authentication information.Teams Table
Stores team/organization information and Stripe subscription details.Team Members Table
Join table linking users to teams with roles.Activity Logs Table
Tracks user and team activities for audit trails.Invitations Table
Manages team invitation system.Database Commands
All database operations are available as npm scripts:Command Details
Script:
npx tsx lib/db/setup.tsInteractive setup wizard that:- Checks for Stripe CLI
- Sets up PostgreSQL (Docker or remote)
- Configures Stripe keys
- Generates
AUTH_SECRET - Creates
.envfile
Script:
drizzle-kit generateGenerates SQL migration files from schema changes. Run this after modifying lib/db/schema.ts.Script:
drizzle-kit migrateApplies pending migrations to your database. Safe to run multiple times.Script:
npx tsx lib/db/seed.tsSeeds the database with:- Test user:
[email protected]/admin123(owner role) - Test team: “Test Team”
- Stripe products: Base (12/month)
Script:
drizzle-kit studioLaunches Drizzle Studio - a web-based database GUI at https://local.drizzle.studioDatabase Connection
The database client is initialized inlib/db/drizzle.ts:
lib/db/drizzle.ts
The application will throw an error on startup if
POSTGRES_URL is not set. Make sure your .env file is properly configured.Seeding the Database
The seed script (lib/db/seed.ts) creates initial data for development and testing:
What Gets Seeded
1. Test User- Email:
[email protected] - Password:
admin123 - Role:
owner
- Name: “Test Team”
- Test user is added as owner
- Price: $8/month
- Trial: 7 days
- Price: $12/month
- Trial: 7 days
Running the Seed Script
- Connect to your database
- Create the test user and team
- Create Stripe products and prices
- Exit with success or error status
Migrations Workflow
When you modify the database schema:-
Edit the schema in
lib/db/schema.ts -
Generate migration:
This creates SQL files in
lib/db/migrations/ - Review the migration (recommended): Check the generated SQL to ensure it matches your intentions
-
Apply the migration:
-
Update TypeScript types:
Drizzle automatically infers types from your schema. Import and use them:
Using Drizzle Studio
Drizzle Studio provides a visual interface to browse and edit your database:- Browse all tables and records
- Run queries
- Edit data directly
- Explore relationships
- View schema information
Drizzle Studio runs locally and connects directly to your database using the
POSTGRES_URL from your .env file.Production Considerations
Connection Pooling
For production deployments, consider using connection pooling:Migrations in Production
Options for running migrations in production:- Manual deployment: Run
pnpm db:migrateduring deployment - Build-time: Add migration to your build script
- Automated: Use a database migration service
Backup Strategy
Regularly backup your production database:- Enable automated backups on your database provider
- Test restore procedures
- Store backups in multiple locations
- Keep backups for at least 30 days
Troubleshooting
Connection Refused
If you see “connection refused” errors:- Verify Docker container is running:
docker ps - Check the port is correct (54322 for local setup)
- Ensure
POSTGRES_URLin.envis correct
Migration Errors
If migrations fail:- Check database connection
- Ensure no syntax errors in schema
- Review the generated SQL in
lib/db/migrations/ - Check for conflicting migrations
Seed Script Fails
Common issues:- Database not migrated: Run
pnpm db:migratefirst - Stripe API error: Verify
STRIPE_SECRET_KEYis set and valid - Duplicate data: Drop and recreate database if needed
Next Steps
Environment Variables
Configure all required environment variables
Stripe Setup
Set up payment processing and subscriptions