- Fix "no pg_hba.conf entry" error by disabling SSL for Docker network - Use scram-sha-256 authentication method for better security - Update knexfile.js to support SSL configuration via environment variable - Add documentation about PostgreSQL connection requirements The PostgreSQL container now accepts connections from the Docker network without requiring SSL, which is appropriate for internal container communication. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
3.7 KiB
Production Deployment Fixes
This document describes the fixes applied to resolve production deployment issues in Docker.
Issues Fixed
1. Database Connection Error: "getaddrinfo ENOTFOUND postgres"
Problem: The backend was trying to connect to hostname "postgres" but the database service is named "db" in docker-compose. Solution:
- Updated
knexfile.jsto use correct default host "db" instead of "postgres" - Added
depends_on: dbto backend service in docker-compose.prod.yml
2. Backend Starting Before Database Ready
Problem: Backend service started before PostgreSQL was ready, causing connection failures. Solution:
- Created
wait-for-db.shscript that waits for PostgreSQL to be ready - Updated Dockerfile to install postgresql-client and use the wait script
- Script also runs migrations automatically on startup
3. Email Processor Initialization Failure
Problem: Email processor tried to initialize on module load before database was available. Solution:
- Modified
emailProcessor.jsto export initialization functions - Updated
server.jsto call initialization after database is ready - Added proper error handling for email service initialization
4. Missing Environment Variables
Problem: Critical storage path environment variables were missing. Solution:
- Added STORAGE_PATH, EVENTS_PATH, and ARCHIVE_PATH to docker-compose.prod.yml
- Created
.env.exampledocumenting all required environment variables
5. Enhanced Health Check
Problem: Basic health check didn't verify database connectivity. Solution:
- Updated
/api/healthendpoint to check database connection - Returns proper HTTP 503 status when unhealthy
Files Modified
- backend/knexfile.js - Fixed production database defaults
- backend/wait-for-db.sh - Created database wait script
- backend/Dockerfile - Added postgresql-client and wait script
- docker-compose.prod.yml - Added dependencies and environment variables
- backend/src/services/emailProcessor.js - Disabled auto-initialization
- backend/server.js - Added email initialization and improved health check
- backend/.env.example - Created environment variable documentation
Deployment Steps
- Ensure all environment variables are set according to
.env.example - Build and deploy with docker-compose:
docker-compose -f docker-compose.prod.yml build docker-compose -f docker-compose.prod.yml up -d - The backend will now:
- Wait for PostgreSQL to be ready
- Run migrations automatically
- Initialize all services in proper order
- Provide health status at
/api/health
Verification
Check deployment health:
curl http://localhost/api/health
Expected response:
{
"status": "ok",
"database": "connected",
"timestamp": "2025-07-13T20:30:00.000Z"
}
Email Configuration
Email service requires configuration in the database. If email is not configured:
- The service will log a warning but continue running
- Emails will be queued but not sent
- Configure email settings in the admin panel after deployment
PostgreSQL Connection Fix
Issue: "no pg_hba.conf entry for host"
This error occurs when PostgreSQL requires SSL but the client connects without encryption.
Solution:
- Disabled SSL requirement for PostgreSQL in Docker environment (
ssl=off) - Added proper authentication method (
scram-sha-256) - This is acceptable for internal Docker networks where all traffic is isolated
Security Note:
For production deployments exposed to the internet:
- Use SSL certificates for PostgreSQL
- Or ensure the database is only accessible within the Docker network
- Never expose PostgreSQL port (5432) directly to the internet