Database Connection Strings & Methods: PostgreSQL, MySQL, MongoDB | Nife

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#

  1. Go to Databases page
  2. Click database name
  3. Click Connection Details
  4. 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 TypePool SizeMax Connections
Small app2-510
Medium app5-1020
Large app10-2050
Enterprise20+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:

  1. Verify hostname is correct
  2. Verify port is correct (5432 for PostgreSQL, 3306 for MySQL)
  3. Check credentials
  4. Verify database exists
  5. Check firewall/network rules
  6. Ensure application IP is whitelisted

Problem: Authentication Failed#

Symptoms:

Error: Password authentication failed
Error: Access denied for user 'root'

Solutions:

  1. Verify password is correct
  2. Check username is correct (usually 'root')
  3. Verify database name
  4. Reset password if forgotten
  5. Copy password directly from dashboard

Problem: Timeout#

Symptoms:

Error: Connection timeout
Error: Timeout connecting to database

Solutions:

  1. Check network connectivity
  2. Increase timeout value in connection
  3. Verify database is running
  4. Check firewall rules
  5. Contact support if database is down

Problem: Too Many Connections#

Symptoms:

Error: Too many connections
Error: Connection limit exceeded

Solutions:

  1. Enable connection pooling
  2. Close unused connections
  3. Reduce pool size if needed
  4. Scale up database plan
  5. 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#

  1. Managing Databases - Scale and monitor
  2. Backup & Recovery - Data protection
  3. Database Best Practices - Optimization tips

Support#

Connection issues?

  • Check sections above
  • Verify credentials
  • Test with command line first
  • Contact support: [email protected]