How to Safely Use AI Coding Assistants with Database Access
I watched in horror as my AI coding assistant cheerfully announced it was about to run a migration on my production database. “Wait, stop!” I typed frantically, but it was too late. The DROP TABLE command had already executed.
Okay, that didn’t actually happen to me. But it’s a legitimate fear that many developers have when using AI coding assistants like Claude Code, Cursor IDE, or GitHub Copilot. These tools are incredibly powerful, but they can also be dangerous if given unrestricted access to your databases.
So how do you safely use AI coding assistants when working with databases?
The Golden Rule: Never Develop in Production
This principle existed long before AI coding assistants:
"You never develop in prod. Ai or no ai."Whether you’re writing code manually or using AI assistance, production should always be off-limits for development work. This isn’t just about AI safety; it’s fundamental software engineering practice.
Environment Isolation is Non-Negotiable
Your development and production environments must be completely isolated:
"Dev and prod are different, isolated environments.Your LLM is never permitted to touch prod.It can nuke your dev from the face of the earth,but if prod is isolated, no harm, no foul."This means:
- Separate database instances - Your AI assistant should only know about dev/staging databases
- Different credentials - Never store production credentials in your dev environment
- Network isolation - Production should be inaccessible from your development machine
The Migration-Based Approach
When working with databases, never let AI assistants execute changes directly:
"For database stuff the sanest approach is never lettingthe agent touch production directly. Have it generatemigration files or scripts"Here’s how this works in practice:
# migrations/add_user_status.pyfrom alembic import opimport sqlalchemy as sa
def upgrade(): op.add_column('users', sa.Column('status', sa.String(20), nullable=True)) op.execute("UPDATE users SET status = 'active' WHERE status IS NULL")
def downgrade(): op.drop_column('users', 'status')The AI generates the migration file, but you:
- Review the generated code line by line
- Test it in a local development environment
- Run it through your CI/CD pipeline
- Deploy to production only after thorough review
Backup Protocol: Your Safety Net
Even with perfect isolation, backups are essential:
"Plus, you should be snapshotting and backing up yourprod db/env regularly. Anything else is professionalmalpractice."Your backup strategy should include:
- Daily automated snapshots of production databases
- Point-in-time recovery capability
- Regular restore testing - backups that haven’t been tested aren’t real backups
- Off-site storage for disaster recovery
Practical Setup for AI + Database Work
Here’s a recommended setup for safely using AI coding assistants:
1. Use Environment Variables
# .env.development (safe to share with AI)DATABASE_URL=postgresql://localhost:5432/myapp_dev
# .env.production (NEVER share with AI)DATABASE_URL=postgresql://prod-server:5432/myapp_prod2. Configure Git Ignore
.env.production.env.*.local*.pemsecrets/3. Use Read-Only Database Users for Analysis
CREATE USER ai_readonly WITH PASSWORD 'secure_password';GRANT CONNECT ON DATABASE myapp_dev TO ai_readonly;GRANT USAGE ON SCHEMA public TO ai_readonly;GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_readonly;This allows AI assistants to analyze your database schema without being able to modify data.
4. Implement Guardrails in Your AI Tool
Many AI coding assistants support custom instructions or rules:
When working with databases:- NEVER execute DROP, TRUNCATE, or DELETE without explicit confirmation- Always generate migration files instead of direct SQL- Never connect to databases containing "prod" in the name- Always show the full SQL before executingCode Review is Still Required
AI-generated database code needs the same scrutiny as human-written code:
[ ] Does the migration do what it claims?[ ] Are there any destructive operations (DROP, DELETE)?[ ] Is the rollback/downgrade path correct?[ ] Will this lock tables during execution?[ ] Are indexes being created efficiently?[ ] Is data migration handled correctly?The Principle of Least Privilege
Apply this security principle to your AI assistant’s database access:
- Give it access only to what it needs
- Use database roles with minimal permissions
- Audit all actions performed by the AI
- Never use superuser accounts for AI-driven operations
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments