Connecting to Databases
Connect your applications to managed databases using connection details and authentication.
Getting Connection Details
After Database Creation
You'll receive connection details:
- Hostname:
db-prod-001.database.nifetency.com - Port:
5432(PostgreSQL),3306(MySQL), etc. - Username:
root - Password: Your set password
- Database Name:
postgres(default)
Finding Existing Credentials
- Go to Databases page
- Click database name
- Click Connection Details
- Copy hostname, port, username
Password: Store separately for security.
Connection Methods
Method 1: Connection String
Use connection string format for your database type:
PostgreSQL:
postgresql://root:PASSWORD@hostname:5432/database
MySQL:
mysql://root:PASSWORD@hostname:3306/database
MongoDB:
mongodb://root:PASSWORD@hostname:27017/database
Redis:
redis://root:PASSWORD@hostname:6379
Method 2: Individual Parameters
Provide individual connection parameters:
Hostname: db-prod.database.nifetency.com
Port: 5432
Username: root
Password: YourPassword
Database: postgres
Method 3: Environment Variables
Set connection details as environment variables:
Example:
export DB_HOST="db-prod.database.nifetency.com"
export DB_PORT="5432"
export DB_USER="root"
export DB_PASSWORD="YourPassword"
export DB_NAME="postgres"
Application Examples
Node.js (PostgreSQL)
const { Pool } = require('pg');
const pool = new Pool({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
// Query
pool.query('SELECT * FROM users', (err, res) => {
console.log(res.rows);
});
Python (PostgreSQL)
import psycopg2
conn = psycopg2.connect(
host="db-prod.database.nifetency.com",
port="5432",
user="root",
password="YourPassword",
database="postgres"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
cursor.close()
conn.close()
Python (MongoDB)
from pymongo import MongoClient
client = MongoClient(
'mongodb://root:[email protected]:27017/'
)
db = client['myapp']
users = db['users']
# Find documents
result = users.find_one({'name': 'John'})
print(result)
Node.js (MongoDB)
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://root:[email protected]:27017/';
MongoClient.connect(url, (err, client) => {
if (err) throw err;
const db = client.db('myapp');
db.collection('users').findOne({}, (err, result) => {
console.log(result);
});
});
Docker Compose
version: '3'
services:
app:
image: myapp:latest
environment:
DB_HOST: db-prod.database.nifetency.com
DB_PORT: 5432
DB_USER: root
DB_PASSWORD: YourPassword
DB_NAME: postgres
Kubernetes
apiVersion: v1
kind: ConfigMap
metadata:
name: db-config
data:
DB_HOST: "db-prod.database.nifetency.com"
DB_PORT: "5432"
DB_USER: "root"
DB_NAME: "postgres"
---
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
stringData:
DB_PASSWORD: "YourPassword"
Connection Tools
GUI Database Clients
For PostgreSQL/MySQL:
- DBeaver: Free, feature-rich, cross-platform
- MySQL Workbench: Official MySQL client
- pgAdmin: Web-based PostgreSQL admin
- HeidiSQL: Free MySQL/PostgreSQL client
For MongoDB:
- MongoDB Compass: Official client
- NoSQL Booster: Advanced client
- Robo 3T: Community MongoDB client
For Redis:
- Redis Desktop Manager: GUI client
- RedisInsight: Official Redis client
Command Line Tools
PostgreSQL:
psql -h db-prod.database.nifetency.com -U root -d postgres
MySQL:
mysql -h db-prod.database.nifetency.com -u root -p
MongoDB:
mongosh -h db-prod.database.nifetency.com:27017 -u root -p
Redis:
redis-cli -h db-prod.database.nifetency.com -p 6379
Testing Connection
Test Before Using
Always test connection before deploying:
Bash script to test:
#!/bin/bash
echo "Testing database connection..."
PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1"
if [ $? -eq 0 ]; then
echo "✓ Connection successful"
else
echo "✗ Connection failed"
exit 1
fi
Python test:
import psycopg2
try:
conn = psycopg2.connect(
host="db-prod.database.nifetency.com",
user="root",
password="YourPassword",
database="postgres"
)
cur = conn.cursor()
cur.execute("SELECT 1")
print("✓ Connection successful")
cur.close()
conn.close()
except Exception as e:
print(f"✗ Connection failed: {e}")
Connection Pool Configuration
What is Connection Pooling?
Connection pooling reuses database connections instead of creating new ones each time. This improves performance and reduces resource usage.
Recommended Pool Sizes
| Application Type | Pool Size | Max Connections |
|---|---|---|
| Small app | 2-5 | 10 |
| Medium app | 5-10 | 20 |
| Large app | 10-20 | 50 |
| Enterprise | 20+ | 100+ |
Configuring Pool (Node.js Example)
const pool = new Pool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
max: 20, // max connections
idleTimeoutMillis: 30000, // close idle after 30s
connectionTimeoutMillis: 2000,
});
SSL/TLS Connections
Enable Encryption
All connections support SSL/TLS encryption:
PostgreSQL with SSL:
postgresql://root:PASSWORD@hostname:5432/database?sslmode=require
MySQL with SSL:
const connection = mysql.createConnection({
host: 'hostname',
user: 'root',
password: 'PASSWORD',
database: 'mydb',
ssl: 'Amazon RDS'
});
Recommendation: Always use SSL/TLS in production.
Troubleshooting Connection
Problem: Connection Refused
Symptoms:
Error: Connection refused
Error: Cannot connect to [hostname]
Solutions:
- Verify hostname is correct
- Verify port is correct (5432 for PostgreSQL, 3306 for MySQL)
- Check credentials
- Verify database exists
- Check firewall/network rules
- Ensure application IP is whitelisted
Problem: Authentication Failed
Symptoms:
Error: Password authentication failed
Error: Access denied for user 'root'
Solutions:
- Verify password is correct
- Check username is correct (usually 'root')
- Verify database name
- Reset password if forgotten
- Copy password directly from dashboard
Problem: Timeout
Symptoms:
Error: Connection timeout
Error: Timeout connecting to database
Solutions:
- Check network connectivity
- Increase timeout value in connection
- Verify database is running
- Check firewall rules
- Contact support if database is down
Problem: Too Many Connections
Symptoms:
Error: Too many connections
Error: Connection limit exceeded
Solutions:
- Enable connection pooling
- Close unused connections
- Reduce pool size if needed
- Scale up database plan
- Monitor active connections
Security Best Practices
1. Never Hardcode Credentials
Bad:
const password = "MySecurePassword123";
Good:
const password = process.env.DB_PASSWORD;
2. Use Connection Pooling
Reuse connections instead of creating new ones.
3. Use SSL/TLS
Always encrypt connections in production.
4. Restrict Network Access
- Use firewall rules
- Allow only necessary IPs
- Use VPN if possible
5. Change Default Passwords
Don't use root account for applications.
6. Monitor Access
- Check connection logs
- Monitor for unusual activity
- Set up alerts
7. Rotate Credentials
- Change passwords periodically
- After team changes
- If credentials exposed
Next Steps
- Managing Databases - Scale and monitor
- Backup & Recovery - Data protection
- Database Best Practices - Optimization tips
Support
Connection issues?
- Check sections above
- Verify credentials
- Test with command line first
- Contact support: [email protected]