Database Providers
| Environment | Database | Purpose |
|---|---|---|
| Local Development | PostgreSQL 18 | Fast iteration, offline work |
| Production | CockroachDB Serverless | Auto-scaling, automatic backups, PITR |
CockroachDB is PostgreSQL-compatible, so the same Prisma schema works for both environments.
Local Development Setup
1. Install PostgreSQL 18
- Ubuntu/Debian
- macOS
- Windows
2. Create Local Database
If you get a password prompt, use the password you set during PostgreSQL installation.
3. Configure Connection String
Updatebackend/.env.local:
YOUR_PASSWORD with your PostgreSQL password.
4. Initialize Database Schema
Verify with Prisma Studio (optional)
Production Setup (CockroachDB)
1. Create CockroachDB Cluster
Sign up for CockroachDB Cloud
Go to cockroachlabs.cloud and create an account.
Create a Serverless cluster
- Click “Create Cluster”
- Select Serverless tier (free tier available)
- Choose a region close to your users
- Name your cluster (e.g.,
millennium-potters-prod)
Create SQL user
- Go to “SQL Users” tab
- Click “Add User”
- Set username and generate a strong password
- Save the password - it won’t be shown again
2. Configure Production Environment
Createbackend/.env.production:
Maximum number of connections in the pool. Set to 20-30 for 50-100 concurrent users.
Connection acquisition timeout in seconds.
3. Apply Schema to Production
Review migration script
Open
migration_plan.sql and verify:- ✅
CREATE TABLEstatements (safe) - ✅
ALTER TABLE ... ADD COLUMN(safe) - ⚠️
ALTER TABLE ... DROP COLUMN(review carefully) - ❌
DROP TABLE(investigate immediately)
Prisma Commands Reference
Development Commands
Production Commands
All production commands should use
DIRECT_URL (without connection pooling) to avoid migration issues.Database Schema Overview
The Millennium Potters schema includes:Core Models
- User - Staff accounts (Admin, Supervisor, Credit Officer)
- Union - Groups of members managed by a Credit Officer
- UnionMember - Individual borrowers
- LoanType - Loan product definitions
- Loan - Loan applications and records
Financial Models
- RepaymentScheduleItem - Payment due dates
- Repayment - Actual payments received
- RepaymentAllocation - Links payments to schedule items
Supporting Models
- DocumentType - Document categories
- UnionMemberDocument - Member identity documents
- LoanDocument - Loan collateral documents
- BackupRecord - Backup metadata
- AuditLog - Activity tracking
- CompanySetting - System configuration
Key Schema Features
- Soft Deletes: All models use
deletedAtinstead of hard deletes - Audit Trail: Full history via
AuditLogand reassignment tables - Connection Pooling: Configured in
datasourceblock - Enums: Strongly typed status fields
Connection String Format
PostgreSQL (Local)
CockroachDB (Production)
Always use
verify-full for production CockroachDB connections.Schema Migration Process
For New Features
Backup and Recovery
Automated Backups (CockroachDB)
CockroachDB Serverless includes:- Automatic hourly backups (last 30 days)
- Point-in-Time Recovery (PITR) to any point in the last 30 days
- No configuration required
Manual Backups
- PostgreSQL (Local)
- CockroachDB (Production)
Application-Level Backups
The system includes a built-in backup feature:- Location: Cloudinary (cloud storage)
- Format: JSON export of all tables
- Trigger: Manual via Settings → Backup or scheduled
- Retention: Configurable (default 30 days)
Troubleshooting
Connection refused errors
Connection refused errors
Symptoms:
Error: connect ECONNREFUSED 127.0.0.1:5432Solutions:- Check PostgreSQL is running:
sudo systemctl status postgresql - Start PostgreSQL:
sudo systemctl start postgresql - Verify port 5432 is open:
sudo netstat -plnt | grep 5432
Authentication failed
Authentication failed
Symptoms:
password authentication failed for user "postgres"Solutions:- Verify password in connection string
- Reset PostgreSQL password:
Database does not exist
Database does not exist
Symptoms:
database "millenium_local" does not existSolutions:Prisma schema out of sync
Prisma schema out of sync
Symptoms:
The schema is not in sync with the databaseSolutions:- In development:
npx prisma db push - In production: Generate and apply migration
- Emergency:
npx prisma db pull(overwrites schema with database state)
CockroachDB connection timeout
CockroachDB connection timeout
Symptoms: Connection hangs or times outSolutions:
- Check SSL mode: must be
verify-full - Verify cluster is running in CockroachDB dashboard
- Check firewall allows outbound connections on port 26257
- Test connection:
psql "$DATABASE_URL" -c "SELECT 1;"
Too many connections error
Too many connections error
Symptoms:
remaining connection slots are reservedSolutions:- Reduce
connection_limitinDATABASE_URL - Check for connection leaks in application code
- Restart application to close stale connections
- Monitor connection count in CockroachDB dashboard
Performance Tuning
Connection Pool Sizing
connection_limit = (expected_concurrent_users * 0.2) + 10
Index Optimization
The schema includes indexes on:- Primary keys (automatic)
- Foreign keys (automatic)
- Frequently queried fields (
email,loanNumber,deletedAt) - Composite indexes for complex queries
Next Steps
Production Deployment
Deploy the application to production
Environment Variables
Review environment configuration
