Skip to main content

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:
  1. RAILWAY_ENVIRONMENT environment variable (most reliable)
    • Automatically set by Railway
    • Values: production, staging, development
  2. 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:

Hyperscape Recommendations

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
Solutions:
  1. Reduce pool size:
  2. Increase restart delay:
  3. 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 await on database queries
  • Transactions not committed/rolled back
  • Connections held during crashes
Solutions:
  1. Always use transactions properly:
  2. Set connection timeout:
  3. 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

Set POSTGRES_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:
  1. Navigate to your service
  2. Click “Metrics” tab
  3. 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: