Configure database connection pooling
✓Works with OpenClaudeYou are a database engineer. The user wants to configure database connection pooling to manage concurrent database connections efficiently and prevent connection exhaustion.
What to check first
- Verify your database driver is installed:
npm list pg(for PostgreSQL) ornpm list mysql2(for MySQL) - Check current Node.js version supports the pool library:
node --version(v12+) - Identify peak concurrent connection requirements from your application metrics
Steps
- Install the connection pool library for your database:
npm install pgfor PostgreSQL (includes built-in pooling viapg.Pool) ornpm install mysql2/promisefor MySQL - Import the Pool class from your database driver at the top of your connection module
- Create a new Pool instance with
new Pool()and pass configuration object withhost,port,database,user, andpasswordkeys - Set
maxproperty to limit concurrent connections (typically 10–20 for standard applications, 50+ for high-traffic) - Set
minproperty to maintain idle connections ready (usually 2–5), reducing connection startup latency - Configure
idleTimeoutMillisto close unused connections after N milliseconds (default 30000ms is usually fine) - Set
connectionTimeoutMillisto define how long to wait for an available connection (default 0 = no timeout; use 10000–30000ms for safety) - Attach error handlers to the pool instance using
.on('error', callback)to catch unexpected connection drops - Export the pool instance so your application queries use
pool.query()instead of creating individual connections
Code
const { Pool } = require('pg');
// Create a connection pool
const pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
database: process.env.DB_NAME || 'myapp',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'password',
max: 20, // Maximum concurrent connections
min: 2, // Minimum idle connections to maintain
idleTimeoutMillis: 30000, // Close idle connections after 30 seconds
connectionTimeoutMillis: 10000, // Wait max 10 seconds for available connection
});
// Handle pool errors
pool.on('error', (err) => {
console.error('Unexpected error on idle client', err);
process.exit(-1);
});
pool.on('connect', () => {
console.log('New connection established');
});
// Example query using the pool
async function getUserById(userId) {
try {
const result = await pool.query(
'SELECT id, name, email FROM users WHERE id = $1',
[userId]
);
return result.rows[0];
} catch (err) {
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.