Railway Database Configuration
Railway uses connection pooling (pgbouncer) which requires special configuration for Hyperscape deployments. This guide covers automatic detection, configuration, and troubleshooting.Automatic Detection
Hyperscape automatically detects Railway deployments and applies appropriate database configuration.Detection Methods
Railway is detected via:-
RAILWAY_ENVIRONMENT environment variable (most reliable)
- Automatically set by Railway
- Values:
production,staging,development
-
Hostname patterns:
.rlwy.net- Railway proxy connections.railway.app- Direct Railway connections.railway.internal- Internal Railway connections
Automatic Configuration
When Railway is detected, Hyperscape automatically:- Disables prepared statements (not supported by pgbouncer)
- Uses lower connection pool limits (max: 6 connections)
- Prevents “too many clients already” errors
Configuration
Environment Variables
Add these to your Railway service environment variables:PM2 Configuration
If using PM2 on Railway, configure restart delays to allow connections to close:Connection Pool Limits
Railway Proxy Limits
Railway’s pgbouncer proxy has strict connection limits:| Deployment Type | Max Connections | Recommended Pool Max |
|---|---|---|
| Hobby Plan | 20 | 3-6 |
| Pro Plan | 100 | 6-10 |
| Team Plan | 200 | 10-20 |
Hyperscape Recommendations
| Scenario | POSTGRES_POOL_MAX | POSTGRES_POOL_MIN | Notes |
|---|---|---|---|
| Stable deployment | 6 | 0 | Default for Railway |
| Crash loops | 3 | 0 | Prevents connection exhaustion |
| Duel arena | 1 | 0 | Minimal connections for streaming |
| Development | 10 | 2 | Higher limits for local testing |
Troubleshooting
”too many clients already” Error
Symptom: PostgreSQL error 53300 during deployment or crashes Causes:- Connection pool too large for Railway limits
- Connections not closing before restart
- Multiple instances competing for connections
-
Reduce pool size:
-
Increase restart delay:
-
Check active connections:
Prepared Statement Errors
Symptom: Errors like “prepared statement does not exist” Cause: Prepared statements not supported by pgbouncer Solution: Automatic - Hyperscape disables prepared statements when Railway is detected. If you see this error, verify Railway detection is working:Connection Leaks
Symptom: Connections not being released, pool exhaustion over time Causes:- Missing
awaiton database queries - Transactions not committed/rolled back
- Connections held during crashes
-
Always use transactions properly:
-
Set connection timeout:
-
Monitor connection count:
Best Practices
1. Use Minimal Connection Pools
Railway’s pgbouncer is designed for many clients with few connections each:2. Don’t Hold Idle Connections
SetPOSTGRES_POOL_MIN=0 to release connections when not in use:
3. Increase Restart Delays
Allow connections to close before PM2 restarts:4. Monitor Connection Usage
Check Railway metrics regularly:- Active connections
- Connection pool utilization
- Query performance
- Error rates
Migration from Other Platforms
From Neon/Supabase
Railway uses pgbouncer, while Neon/Supabase may use different poolers:From Direct PostgreSQL
Direct PostgreSQL connections support higher limits:Advanced Configuration
Connection String Parameters
Railway supports additional connection parameters:Drizzle ORM Configuration
Hyperscape uses Drizzle ORM with automatic Railway detection:Monitoring
Railway Dashboard
Monitor database health in Railway dashboard:- Navigate to your service
- Click “Metrics” tab
- Check:
- Active connections
- Query latency
- Error rates
- CPU/Memory usage
Application Logs
Enable database logging in your application:Health Checks
Add database health check endpoint:Related Documentation
- Railway Dev/Prod Setup - Full Railway deployment guide
- AGENTS.md - PostgreSQL connection pool configuration
- README.md - Troubleshooting Railway errors
- Drizzle ORM Docs - ORM documentation