---
name: database-best-practices
description: Database query safety and connection management
---
## Database Best Practices
- Always use connection pooling — never raw connections
- Close connections and sessions explicitly after use
- Use parameterized queries — never string interpolation for SQL
- Add indexes for frequently queried columns
- Migrations must be idempotent and backward-compatible
- Monitor connection pool usage in production
- Use transactions for multi-step writes
- Set query timeouts to prevent runaway queries
