Overview
Bounty uses PostgreSQL as its primary database with Drizzle ORM for type-safe database operations and schema management. This guide covers setting up your database, running migrations, and managing your data.Database Requirements
Version
PostgreSQL 14 or higher
Storage
Minimum 1GB for initial deployment
SSL/TLS
Required for production deployments
Extensions
No special extensions required
Choosing a Database Provider
Recommended: Neon
Neon provides serverless PostgreSQL with excellent features:- Serverless - Automatic scaling and pay-per-use pricing
- Branching - Database branches for development and testing
- Point-in-time Recovery - Restore to any point in time
- Free Tier - Generous free tier for small projects
Alternative Providers
Supabase
Supabase
Supabase offers PostgreSQL with real-time capabilities:
- Built-in authentication (if not using Better Auth)
- Real-time subscriptions
- Storage and edge functions
- Good free tier
Railway
Railway
Railway provides simple PostgreSQL hosting:
- One-click PostgreSQL deployment
- Automatic backups
- Simple pricing
- Good for small to medium deployments
- Create new project
- Add PostgreSQL service
- Copy connection string from variables
Self-Hosted PostgreSQL
Self-Hosted PostgreSQL
Run your own PostgreSQL instance:Docker:Connection String:
Database Configuration
Connection String Format
Bounty requires a PostgreSQL connection string in theDATABASE_URL environment variable:
username- Database user (e.g.,postgres)password- User password (URL-encode special characters)host- Database server hostnameport- PostgreSQL port (default:5432)database- Database namesslmode=require- Enforce SSL connection (required for production)
Connection Pool Configuration
Bounty uses node-postgres (pg) with connection pooling. The configuration is inpackages/db/src/index.ts:
- SSL is automatically enabled in production
- Connection pooling is handled by pg.Pool
- Drizzle ORM manages query execution
Drizzle ORM Setup
Configuration
Drizzle configuration is defined inpackages/db/drizzle.config.ts:
- out - Directory for migration files
- schema - Path to schema definitions
- dialect - Database type (PostgreSQL)
- tablesFilter - Exclude system tables from schema introspection
Database Schema
The database schema is organized into multiple files inpackages/db/src/schema/:
Core Schemas
Core Schemas
- auth.ts - User authentication (Better Auth tables)
- profiles.ts - User profiles and settings
- bounties.ts - Bounty listings and submissions
- organization.ts - Organizations and memberships
- github-installation.ts - GitHub App installations
Feature Schemas
Feature Schemas
- payments.ts - Payment tracking and transactions
- notifications.ts - User notifications
- invites.ts - Organization invites
- feature-votes.ts - Feature request voting
- passkey.ts - Passkey authentication
- linear-account.ts - Linear integration
- beta-applications.ts - Early access applications
Running Migrations
Initial Setup
-
Set DATABASE_URL
-
Push Schema to Database
This command:
- Reads your schema definitions
- Generates and executes SQL to create tables
- Updates existing tables if schema changed
- Does NOT create migration files
Production Migrations
For production deployments, use proper migrations:Generate Migration
packages/db/src/migrations/ based on schema changes.
Review Migration
Check the generated SQL inpackages/db/src/migrations/XXXX_*.sql:
Run Migration
Migration Workflow
Development:Database Management Commands
All database commands are defined inpackage.json and packages/db/package.json:
Available Commands
Drizzle Studio
Drizzle Studio provides a visual interface to browse and edit your database:https://local.drizzle.studio where you can:
- Browse all tables and data
- Edit records directly
- Run custom queries
- Visualize relationships
Drizzle Studio uses your
DATABASE_URL environment variable. Make sure it’s set before running the command.Database Backups
Manual Backup
Create a database dump:scripts/dump-db.sh to export your database.
pg_dump (Direct)
For more control, use pg_dump directly:Automated Backups
Most providers offer automated backups:- Neon - Automatic point-in-time recovery
- Supabase - Daily automated backups
- Railway - Snapshot backups on demand
Database Monitoring
Performance Monitoring
Monitor these key metrics:- Connection count - Track active connections
- Query performance - Identify slow queries
- Table sizes - Monitor growth over time
- Index usage - Ensure indexes are being used
Query Active Connections
Check Table Sizes
Troubleshooting
Connection Refused
Connection Refused
Error:
ECONNREFUSED or connection refusedSolutions:- Verify database is running
- Check host and port are correct
- Ensure firewall allows connections
- Verify network connectivity
SSL/TLS Errors
SSL/TLS Errors
Error:
SSL connection required or certificate verify failedSolutions:- Add
?sslmode=requireto connection string - For self-signed certs, use
?sslmode=requirewithrejectUnauthorized: false - Ensure your provider supports SSL
Authentication Failed
Authentication Failed
Error:
password authentication failedSolutions:- Verify username and password are correct
- URL-encode special characters in credentials
- Check user has database access permissions
- Ensure user exists in the database
Migration Errors
Migration Errors
Error: Migration fails or schema mismatchSolutions:
- Check existing schema doesn’t conflict
- Review migration SQL for errors
- Ensure no manual schema changes were made
- Drop and recreate database for clean slate (dev only)
Too Many Connections
Too Many Connections
Error:
sorry, too many clients alreadySolutions:- Reduce connection pool size
- Upgrade database tier for more connections
- Check for connection leaks in application
- Use connection pooler like PgBouncer
Advanced Topics
Database Migrations in CI/CD
Run migrations automatically during deployment:Multiple Environments
Use different databases for each environment:Database Seeding
Populate your database with test data:Best Practices
Use Migrations
Always use migrations in production. Never use
db:push for production deployments.Test Migrations
Test migrations on staging before applying to production. Review generated SQL.
Backup First
Always backup before running migrations or making schema changes.
Monitor Performance
Set up monitoring for connection count, query performance, and table sizes.
Next Steps
Deployment
Deploy your Bounty instance to production with database configured