Overview
Better Uptime uses PostgreSQL as its primary database and Prisma as the ORM. This guide covers database setup, schema overview, and running migrations.Prerequisites
- PostgreSQL 12 or higher installed
- Database user with CREATE DATABASE privileges
- Node.js or Bun installed for running Prisma commands
Database Configuration
PostgreSQL Setup
Connection String Examples
Prisma Schema Overview
Better Uptime uses Prisma for database management. The schema is located atpackages/store/prisma/schema.prisma.
Core Models
User Model
User Model
Stores user account information:Fields:
id: Unique user identifier (CUID)email: Unique email addresspasswordHash: Hashed password (optional for OAuth users)emailVerified: Email verification statusname: Display nameavatarUrl: Profile picture URLisActive: Account active status
Account Model
Account Model
Stores OAuth provider accounts:Purpose: Links users to OAuth providers (GitHub)
Website Model
Website Model
Stores monitored websites:Fields:
url: Website URL to monitorname: Optional friendly nameisActive: Monitoring enabled/disableduserId: Owner of the monitor
StatusPage Model
StatusPage Model
Stores public status pages:Fields:
name: Status page nameslug: Unique URL slugisPublished: Public visibilitydomain: Custom domain configuration
StatusPageMonitor Model
StatusPageMonitor Model
Links websites to status pages:Purpose: Many-to-many relationship between status pages and monitors
StatusPageDomain Model
StatusPageDomain Model
Stores custom domain configuration:Purpose: Custom domain verification for status pages
EmailVerificationToken Model
EmailVerificationToken Model
Stores email verification tokens:Purpose: Email verification during user registration
Enums
Status of uptime checksValues:
UP: Website is respondingDOWN: Website is not responding
Custom domain verification statusValues:
PENDING: Verification in progressVERIFIED: Domain verified successfullyFAILED: Verification failed
Running Migrations
Initial Setup
Generate Prisma Client
packages/store/generated/prisma/.Verify database
Common Prisma Commands
Database Indexes
The schema includes optimized indexes for common queries:Indexes improve query performance but use additional disk space. These indexes are optimized for Better Uptime’s query patterns.
Database Relationships
Key Relationships:- Users can have multiple OAuth accounts
- Users can create multiple websites and status pages
- Status pages can contain multiple monitors
- Monitors link websites to status pages (many-to-many)
- Status pages can have one custom domain
Backup and Restore
Backup Database
Restore Database
Automated Backups
Create a cron job for daily backups:Troubleshooting
Connection Refused
Connection Refused
Error:
Error: connect ECONNREFUSED 127.0.0.1:5432Solutions:-
Verify PostgreSQL is running:
-
Check PostgreSQL is listening:
-
Verify connection string in
.env
Authentication Failed
Authentication Failed
Error:
Error: password authentication failed for userSolutions:-
Verify username and password in
DATABASE_URL -
Check PostgreSQL authentication method in
pg_hba.conf: -
Change authentication to
md5orscram-sha-256 -
Restart PostgreSQL:
Migration Failures
Migration Failures
Error:
Migration failed to applySolutions:-
Check migration status:
-
Resolve failed migration:
-
If all else fails, reset (⚠️ deletes all data):
SSL Required
SSL Required
Error:
no pg_hba.conf entry for host, SSL offSolution: Add ?sslmode=require to connection string:Production Best Practices
Connection Pooling
Connection Pooling
Use connection pooling for better performance:Or use Prisma’s connection pool settings:
Regular Backups
Regular Backups
- Automate daily backups
- Store backups off-site
- Test restore procedures regularly
- Keep at least 30 days of backups
Monitoring
Monitoring
Monitor database health:
- Query performance
- Connection count
- Disk usage
- Replication lag (if using replicas)
Security
Security
- Use strong passwords
- Enable SSL/TLS connections
- Restrict network access
- Keep PostgreSQL updated
- Use read-only replicas for reporting
Next Steps
Development Setup
Set up local development environment
Production Deployment
Deploy to production
Environment Variables
Configure all environment variables