Skip to content

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:

Reddit comment on r/ClaudeAI
"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:

Environment separation best practice
"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:

  1. Separate database instances - Your AI assistant should only know about dev/staging databases
  2. Different credentials - Never store production credentials in your dev environment
  3. 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:

Safe database workflow with AI
"For database stuff the sanest approach is never letting
the agent touch production directly. Have it generate
migration files or scripts"

Here’s how this works in practice:

Example: AI-generated migration file
# migrations/add_user_status.py
from alembic import op
import 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:

  1. Review the generated code line by line
  2. Test it in a local development environment
  3. Run it through your CI/CD pipeline
  4. Deploy to production only after thorough review

Backup Protocol: Your Safety Net

Even with perfect isolation, backups are essential:

Reddit advice on backups
"Plus, you should be snapshotting and backing up your
prod db/env regularly. Anything else is professional
malpractice."

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

Environment configuration
# .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_prod

2. Configure Git Ignore

Keep production secrets out of version control
.env.production
.env.*.local
*.pem
secrets/

3. Use Read-Only Database Users for Analysis

Creating a read-only user for AI 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:

Example AI guardrail configuration
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 executing

Code Review is Still Required

AI-generated database code needs the same scrutiny as human-written code:

Checklist for reviewing AI-generated migrations
[ ] 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