The balancer application supports connecting to PostgreSQL databases via two methods:
- CloudNativePG - Kubernetes-managed PostgreSQL cluster (within cluster)
- AWS RDS - External PostgreSQL database (AWS managed)
The application automatically detects the connection type based on the SQL_HOST environment variable format.
The application determines the connection type by analyzing the SQL_HOST value:
-
CloudNativePG:
- Contains
.svc.cluster.local(Kubernetes service DNS) - Short service name (e.g.,
balancer-postgres-rw) - Typically no SSL required within cluster
- Contains
-
AWS RDS:
- Full domain name (e.g.,
balancer-db.xxxxx.us-east-1.rds.amazonaws.com) - External IP address
- Typically requires SSL
- Full domain name (e.g.,
All database configuration is done via environment variables:
SQL_ENGINE: Database engine (default:django.db.backends.postgresql)SQL_DATABASE: Database nameSQL_USER: Database usernameSQL_PASSWORD: Database passwordSQL_HOST: Database host (see examples below)SQL_PORT: Database port (default:5432)SQL_SSL_MODE: Optional SSL mode (see SSL Configuration below)
When using CloudNativePG, the application connects to the Kubernetes service created by the operator.
Example Configuration:
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=balancer
SQL_USER=balancer
SQL_PASSWORD=<password-from-secret>
SQL_HOST=balancer-postgres-rw
SQL_PORT=5432Service Names:
{cluster-name}-rw: Read-write service (primary instance){cluster-name}-r: Read service (replicas){cluster-name}-ro: Read-only service
Full DNS Name:
SQL_HOST=balancer-postgres-rw.balancer.svc.cluster.localWhen using AWS RDS, the application connects to the external RDS endpoint.
Example Configuration:
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=balancer
SQL_USER=balancer
SQL_PASSWORD=<rds-password>
SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com
SQL_PORT=5432
SQL_SSL_MODE=requireWhen using Docker Compose for local development, the application connects to the db service container.
Example Configuration:
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=balancer_dev
SQL_USER=balancer
SQL_PASSWORD=balancer
SQL_HOST=db
SQL_PORT=5432SSL is typically not required for connections within the Kubernetes cluster. The application will not use SSL by default for CloudNativePG connections.
SSL is typically required for AWS RDS connections. The application defaults to require mode for external hosts, but you can override this:
SSL Mode Options:
disable: No SSLallow: Try non-SSL first, then SSLprefer: Try SSL first, then non-SSL (default for external)require: Require SSLverify-ca: Require SSL and verify CAverify-full: Require SSL and verify CA and hostname
Example:
SQL_SSL_MODE=require-
Update the
SQL_HOSTenvironment variable in your SealedSecret:# Old (AWS RDS) SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com # New (CloudNativePG) SQL_HOST=balancer-postgres-rw
-
Update database credentials to match CloudNativePG secret
-
Remove or set
SQL_SSL_MODEtodisable(optional, as it's auto-detected) -
Restart the application pods
-
Update the
SQL_HOSTenvironment variable:# Old (CloudNativePG) SQL_HOST=balancer-postgres-rw # New (AWS RDS) SQL_HOST=balancer-db.xxxxx.us-east-1.rds.amazonaws.com
-
Update database credentials to match RDS credentials
-
Set
SQL_SSL_MODE=require(or appropriate mode) -
Ensure network connectivity (VPC peering, security groups, etc.)
-
Restart the application pods
-
Verify host format: Check that
SQL_HOSTmatches the expected format for your connection type -
Check network connectivity:
- CloudNativePG: Ensure pods are in the same namespace
- AWS RDS: Verify VPC peering, security groups, and network ACLs
-
Verify credentials: Ensure username, password, and database name are correct
-
Check SSL configuration: For AWS RDS, ensure SSL is properly configured
"Connection refused"
- Verify the host and port are correct
- Check if the database service is running
- Verify network connectivity
"SSL required"
- Add
SQL_SSL_MODE=requirefor AWS RDS connections - Verify SSL certificates are available
"Authentication failed"
- Verify username and password
- Check database user permissions
- Ensure the database exists