1
0 Comments

Setting Up Claude Code With Safe Production Database Access

There is a spectrum from "AI writes everything and runs it" to "humans write everything by hand." Almost every team I see picks one of the two endpoints. Both are wrong for SQL.

The right point is in the middle. The AI drafts the query. A human reads it before it runs. That sounds obvious until you watch how few tools actually default to it.

Why fully automatic is bad

The case for fully automatic SQL generation is "it's faster." That's true and not enough.

Two things go wrong without a review step.

First, the AI silently picks the wrong join semantics and you don't catch it. Claude can correctly write a LEFT JOIN orders 95% of the time. The 5% it writes INNER JOIN orders you lose every customer who hasn't placed an order yet. The query runs. The number is wrong. Nobody knows because it's a number, not an exception.

Second, write operations get scary. You said "delete that test user." The agent runs DELETE FROM users WHERE email = '[email protected]'. It also runs it without a LIMIT 1, and it turns out three test users had that email across three environments because of an old data import you forgot about. The "test user" you wanted to delete is gone. The other two are also gone, and one of them was a real customer who happened to use that email five years ago.

This isn't theoretical. I've watched it happen in slack threads. The agent did exactly what was asked. The ask was wrong. There was no chance to catch it.

Read-only enforcement at the gateway prevents the second problem. You still need a review step for the first.

Why fully manual is also bad

Writing a 25-line query with three CTEs and a window function to answer "which customers placed orders over $100 last month grouped by signup cohort" is a 12-minute job for a senior. Most companies don't have a senior available for those 12 minutes.

So either:

  • The senior writes it and gets pulled out of real work.
  • A non-engineer doesn't ask the question, decides on gut, and the company makes a worse decision.
  • Someone copies an old query, modifies one filter, and ships a subtly broken version because they didn't realize the underlying join had changed.

All three of these happen every week at every series-A startup I've talked to. The cost of "fully manual" isn't that SQL is hard. It's that asking the question becomes a political event, and most questions just don't get asked.

What "AI-assisted" actually means

The model I think makes sense, and the one QueryBear runs:

  1. The user asks a question in plain English.
  2. The AI generates a SQL query against the live schema.
  3. The query is shown to the user, syntax-highlighted, with the AI's reasoning if you want it.
  4. The user can verify, edit, or re-prompt.
  5. On run, the query goes through a security pipeline (parser, allowlist, column masking, row limit, timeout) before hitting the database.
  6. Results come back. Read-only.

There are three reasons this is the right shape.

It's faster than manual. The 12-minute query becomes a 30-second one. Even if the user spends another 30 seconds reading the SQL, you're 10x ahead.

It's safer than fully automatic. The user has a real chance to notice "wait, that's an inner join, not a left." That's the moment that prevents the wrong number.

It's accessible. People who can read SQL but not write it can still verify a query. That's a much larger group than people who can write it from scratch. A PM, a designer, an ops lead, a customer-success manager — most of them can read SELECT email, count(*) FROM orders WHERE created_at > '2026-04-01' GROUP BY email. Almost none of them want to type it.

What you actually need to know to use it

I get pushback from skeptical engineers that AI-assisted SQL "just makes people lazy" and they should learn SQL. I have some sympathy. I also think the empirical floor of useful SQL knowledge has gone down a lot.

The skill you need to use AI-assisted SQL well isn't writing — it's reading. Specifically:

  • Recognize the difference between INNER JOIN, LEFT JOIN, and the silent assumption an INNER JOIN makes.
  • Notice when an aggregate is missing a GROUP BY clause it should have.
  • Spot a missing WHERE filter on a destructive query that the AI happens to have written as a SELECT (in your tool, anyway — never trust this in raw psql).
  • Know that NULL = NULL is NULL, not true, and what that does to your filter.

That's a few hours of reading, not a CS degree. Most people who get burned by AI SQL got burned because they skipped this — not because the AI was bad.

The pattern that wins

The teams getting real value from AI-assisted SQL share a few traits:

  • They keep the AI strictly read-only at the connection level. Writes are a different code path, with explicit confirmations.
  • They review the SQL every time. Yes, every time. It takes 5 seconds.
  • They save the queries they re-run, so the AI isn't re-rolling the same query with subtly different filters every morning.
  • They invest 2-3 hours in teaching non-engineers to read SQL, not write it.

The teams that get burned skip the review and assume the AI is a black box that produces correct numbers. It isn't. It's a typing assistant with a lot of context. Treat it like one.

The reason I'm building QueryBear is that the gap between "fully automatic, fast, sometimes wrong" and "fully manual, slow, gated by engineering" is where most real teams want to live. They just don't have a tool that lets them.

That's the pitch. Use AI to write the SQL. Use your eyes to make sure it's right. Use a gateway to make sure that even if you're wrong, the worst case is a confusing number, not a deleted table.

on May 7, 2026
Trending on Indie Hackers
7 years in agency, 200+ B2B campaigns, now building Outbound Glow User Avatar 105 comments How I built an AI workflow with preview, approval, and monitoring User Avatar 57 comments The "Book a Demo" Button Was Killing My Pipeline. Here's What I Replaced It With. User Avatar 45 comments I built a desktop app to move files between cloud providers without subscriptions or CLI User Avatar 26 comments Show IH: I built an AI agent that helps founders find the right people User Avatar 24 comments My AI bill was bleeding me dry, so I built a "Smart Meter" for LLMs User Avatar 20 comments