Overview
Tresa Contafy uses PostgreSQL 12+ as its database with Sequelize as the ORM. This guide covers database setup, configuration, and connection management.Requirements
PostgreSQL
Version 12 or higher required
Sequelize
ORM for database migrations and models
Local Development Setup
Production Setup
Railway (Recommended)
Create PostgreSQL Service
In your Railway project:
- Click “New” → “Database” → “Add PostgreSQL”
- Railway will automatically provision the database
Copy Connection String
Railway provides the
DATABASE_URL automatically:- Go to your PostgreSQL service
- Click “Variables” tab
- Copy the
DATABASE_URLvalue
Other Hosting Providers
For AWS RDS, DigitalOcean, or other providers:Database Configuration
Connection Settings
The database configuration is defined insrc/database/config.ts:
Connection Pool Settings
Maximum number of simultaneous connections
Minimum number of connections to keep alive (prevents frequent reconnections)
Maximum time (ms) to acquire a connection before throwing error
Maximum time (ms) a connection can be idle before being released
Time interval (ms) to check for idle connections
SSL Configuration
SSL is automatically enabled when:
DATABASE_URLcontainsrailway.apporrlwy.netNODE_ENVis set toproduction
Database Schema
Main Tables
Tresa Contafy uses the following database tables:users
users
User accounts with authentication and profile information
- Email verification tokens
- Password reset tokens
- Firebase UID (for Google auth)
- Trial and branding settings
profiles
profiles
RFC profiles (Mexican tax IDs) for each user
- Multiple profiles per user
- Fiscal regime information
- SAT download credentials (FIEL)
- Freeze settings for period locking
invoices
invoices
Incoming invoices (facturas) from XML CFDI files
- Tax calculations (IVA, ISR, IEPS)
- Payment type (PUE/PPD)
- SAT validation status
accrued_expenses
accrued_expenses
Outgoing expenses from XML CFDI files
- Payment dates
- Tax deductibility tracking
payment_complements
payment_complements
Payment complement documents (complementos de pago)
- Links to related invoices
- Payment matching for PPD invoices
subscriptions
subscriptions
Stripe subscription management
- Trial periods
- Subscription status
- Plugin access
payment_events
payment_events
Stripe webhook events log
- Payment history
- Subscription changes
Testing Database Connection
Verify your database connection:Troubleshooting
Connection timeout errors
Connection timeout errors
Problem: Database connection times outSolutions:
- Verify
DATABASE_URLis correct - Check firewall rules allow connections
- Ensure database is running and accessible
- Verify SSL settings match provider requirements
SSL/TLS errors
SSL/TLS errors
Problem:
SSL connection required or certificate errorsSolutions:- Add
?sslmode=requireto connection string - Set
rejectUnauthorized: falsefor self-signed certificates - Update
dialectOptions.sslin config
Too many connections
Too many connections
Problem:
Too many connections errorSolutions:- Reduce
pool.maxin configuration - Check for connection leaks in application code
- Upgrade database plan for more connections
ECONNRESET errors
ECONNRESET errors
Problem: Random connection resetsSolutions:
- Enable
keepAlive: trueindialectOptions - Increase
pool.idletimeout - Ensure
pool.minis at least 1
Next Steps
Run Migrations
Initialize database schema with Sequelize migrations
Environment Variables
Configure all required environment variables