Database Connection Configuration
PostgreSQL server hostname or IP address.Examples:
localhost- Local installationpostgres- Docker Compose service namedb.example.com- Remote database server10.0.1.5- IP address
localhostPostgreSQL server port.Default:
5432Name of the PostgreSQL database.Defaults by environment:
- Development:
chatwoot_dev - Test:
chatwoot_test - Production:
chatwoot_production
PostgreSQL user for authentication.Defaults by environment:
- Development:
postgres - Test:
postgres - Production:
chatwoot_prod
Password for PostgreSQL user authentication.Security: Use a strong, unique password for production environments.
Performance Configuration
Maximum number of threads for Rails application.This directly affects the database connection pool size for web processes.Default:
5Formula: Database pool size = RAILS_MAX_THREADS for web processesNumber of concurrent Sidekiq worker threads.This affects the database connection pool size for Sidekiq processes.Default:
10Formula: Database pool size = SIDEKIQ_CONCURRENCY for Sidekiq processesFrequency in seconds to run the connection pool reaper.The reaper attempts to find and recover connections from dead threads, preventing connection leaks.Default:
30Range: 10-60 seconds recommendedMaximum time a database query can run before being terminated.Default:
14sFormat: Number followed by time unit (ms, s, min)Examples:14s- 14 seconds30s- 30 seconds1min- 1 minute
Connection Pool Sizing
Chatwoot automatically adjusts the database connection pool based on the process type: Web processes:Calculating Total Connections
Total database connections required:- 4 web workers with
RAILS_MAX_THREADS=5: 20 connections - 2 Sidekiq processes with
SIDEKIQ_CONCURRENCY=10: 20 connections - Buffer for migrations, console, etc.: 10 connections
- Total required: 50 connections
PostgreSQL Version Requirements
Minimum version: PostgreSQL 12Recommended version: PostgreSQL 14 or higher Chatwoot uses modern PostgreSQL features including:
- JSONB columns
- Full-text search
- Partitioning
- Generated columns
Configuration Examples
Local Development
Docker Compose
Production Deployment
High-Traffic Production
Database Setup
Creating the Database
For a new installation:Database User Setup
Create a dedicated PostgreSQL user for Chatwoot:PostgreSQL Server Configuration
Recommended PostgreSQL server settings for production:postgresql.conf
pg_hba.conf
Managed PostgreSQL Services
AWS RDS PostgreSQL
- Use db.t3.medium or larger for production
- Enable automated backups
- Use Multi-AZ for high availability
- Enable Performance Insights
Google Cloud SQL PostgreSQL
Azure Database for PostgreSQL
DigitalOcean Managed Database
Heroku Postgres
DATABASE_URL which Chatwoot automatically uses.
Backup and Restore
Manual Backup
Restore from Backup
Automated Backups
Set up automated backups using cron:Database Maintenance
Vacuum and Analyze
Regular maintenance improves query performance:Reindex
Rebuild indexes periodically:Monitoring
Key Metrics to Monitor
-
Connection count
-
Long-running queries
-
Database size
-
Table sizes
-
Cache hit ratio
Troubleshooting
Connection refused
Symptoms:could not connect to server: Connection refused
Solutions:
- Verify PostgreSQL is running:
systemctl status postgresql - Check PostgreSQL is listening:
ss -tlnp | grep 5432 - Verify
POSTGRES_HOSTandPOSTGRES_PORTare correct - Check firewall rules allow connections
Authentication failed
Symptoms:FATAL: password authentication failed for user
Solutions:
- Verify username and password are correct
- Check
pg_hba.confallows connection from your IP - Ensure authentication method matches (md5, scram-sha-256)
- Reload PostgreSQL after config changes:
systemctl reload postgresql
Too many connections
Symptoms:FATAL: sorry, too many clients already
Solutions:
- Increase
max_connectionsin postgresql.conf - Reduce connection pool sizes in application
- Check for connection leaks
- Implement connection pooling with PgBouncer
Slow queries
Symptoms: Requests timeout,POSTGRES_STATEMENT_TIMEOUT errors
Solutions:
- Identify slow queries in PostgreSQL logs
- Add missing indexes:
rails db:migrate - Run
VACUUM ANALYZEto update statistics - Increase
POSTGRES_STATEMENT_TIMEOUTif needed - Consider read replicas for heavy SELECT queries
Disk space issues
Symptoms:ERROR: could not extend file
Solutions:
- Check disk space:
df -h - Run
VACUUM FULLto reclaim space - Archive old data
- Increase disk size
Migration failures
Symptoms:rails db:migrate fails
Solutions:
- Check PostgreSQL logs for detailed errors
- Ensure database user has required permissions
- Verify PostgreSQL version meets requirements
- Check for conflicting data before migration
Security Best Practices
- Use strong passwords: Generate complex passwords for database users
- Limit connections: Configure
pg_hba.confto allow only necessary IPs - SSL/TLS: Enable SSL for database connections in production
- Least privilege: Grant only required permissions to application user
- Regular updates: Keep PostgreSQL updated with security patches
- Audit logs: Enable query logging for security monitoring
- Network isolation: Run database in private network, not exposed to internet
Performance Optimization
Connection Pooling with PgBouncer
For high-traffic deployments, use PgBouncer:Read Replicas
For read-heavy workloads, configure read replicas and use Rails multiple databases feature.Related Configuration
- Environment Variables - Complete environment variable reference
- Redis Configuration - Cache and background jobs
- Email Setup - Email configuration

