r/postgres • • 7d ago

Discussion How are you sandboxing local LLMs with SQL access? I open-sourced a local Layer-4 Postgres proxy ("SQL Guillotine") to block hallucinated DROP/DELETE queries

Hey everyone! Lately I've been wiring up AI agents (Gemini CLI / MCP servers, LangChain, CrewAI, and smaller quantized local models) to PostgreSQL so they can actually read and write data—not just run read-only queries.

The scary part is obvious: one bad hallucination and the model runs a `DROP TABLE`, wipes rows with a `DELETE` missing a `WHERE` clause, or gets stuck in an infinite tool-calling loop. Asking a second LLM to double-check every SQL query adds too much latency, and regex filters break easily.

So I built Aegis, an open-source Go proxy + Python SDK that acts like a two-layer seatbelt sitting between your AI agent and Postgres:

• Layer 1 — Instant Copy-on-Write (CoW) Sandboxes: Using a one-line Python decorator (`@safe_db_run` in `aegis-sdk`), Aegis can spin up an instant, throwaway Copy-on-Write database branch (via Neon) for each agent run. Your agent gets full read/write freedom on an isolated clone, and the branch self-destructs when the run finishes so production data is never touched.

• Layer 2 — "The SQL Guillotine" (100% Local Docker Proxy): You just point your Postgres connection string to port `5433` instead of `5432`—zero changes to your app or MCP config. Aegis parses every query in memory using Postgres's real parser (compiled to WebAssembly) before your DB ever sees it. Normal queries pass right through, while `DROP TABLE`, `TRUNCATE`, or unfiltered `DELETE` statements get severed immediately.

• Runaway Loop Protection (Offline): If an agent gets stuck repeating queries in a loop, Aegis enforces local rate limits via a simple `policies.yaml` file (no cloud dependencies or Redis required) so it won't lock up your machine or burn through DB compute.

GitHub Repo (100% open-source): https://github.com/raymondhani/aegis-proxy

Curious how everyone else here is safely giving Gemini / MCP / local agents write access to databases right now—are you spinning up throwaway containers/branches, sticking strictly to read-only views, or building custom tool wrappers?

0 Upvotes

5 comments sorted by

1

u/elevarq 7d ago

Missing a WHERE condition is easy to check, but what about having the wrong WHERE condition?

DELETE FROM my_table WHERE logdate > '2025-01-01';
DELETE FROM my_table WHERE logdate < '2025-01-01';
DELETE FROM my_table WHERE logdate < '2027-01-01';

Which one is the right one? They can all be right, depending on the context.

1

u/RaymondArtin 7d ago

You're completely right — Aegis does NOT solve that out of the box on the wire, because an AST parser only understands SQL structure, not business intent. Since all three of your DELETE queries have a valid WHERE clause, the proxy sees them as syntactically scoped even if > '2025-01-01' wipes almost the whole table by mistake.

Here is where the boundary actually sits in the repo today:

  1. What the Go Wasm AST proxy catches (Port 5433): Unambiguous structural footguns — DROP, TRUNCATE, DELETE with no WHERE clause at all, and runaway query loops via local rate limits. Because the Go proxy engine is open-source, you can extend the AST checks with static schema invariants (for example, blocking any DELETE on my_table unless logdate is older than a fixed window), which helps when one direction is never valid.
  2. When any of the three queries could legitimately be right depending on context: Static AST rules can't guess intent. That's why the Python SDK pairs the proxy with optional Copy-on-Write ephemeral branches (so a bad WHERE predicate only hits a throwaway DB clone you can inspect/diff first), and on production tables nothing replaces native Postgres RLS, narrow role permissions, or locking agents behind scoped stored procedures.

In practice, when an agent genuinely needs conditional write/delete access like your logdate example, would you handle that strictly through parameterized SQL functions on Postgres, or add a transaction row-count circuit breaker that rolls back if affected rows spike?

1

u/elevarq 7d ago

An AI agent should imho not have the permissions to update or delete data. Changing the structure of the database is even worse, your applications won’t work without changing their SQL statements.

Would you give a junior engineer full control over a production database? Unlikely. But an AI agent that doesn’t understand a damn thing about your business, and doesn’t care about any damage, does get full control?

Hmmm…

Counting down the days until you’re out of business

1

u/RaymondArtin 7d ago

Agreed on the junior engineer rule — you wouldn't hand a junior dev raw prod superuser keys either, you give them an isolated clone to work against and block destructive DDL, which is the exact workflow the ephemeral Copy-on-Write branches + port 5433 proxy enforce.

In a perfect world every dev locks down custom Postgres roles and stored procedures before wiring up an MCP server or local coding agent, right up until someone inevitably pastes their default dev/staging DATABASE_URL into an agent config to move fast. Aegis is the seatbelt for those setups, not a license to hand an LLM superuser access on prod — appreciate the pushback though, good reminder to keep "least-privilege Postgres roles first" front and center in the README.

1

u/elevarq 7d ago

Not even a clone is helpful, it has no workload. Besides that, how are you going to test the results if the intent of the statement is unknown?

Juniors are a risk, and AI agents are a faster implementation of the juniors. No matter what you do, your risk levels go up. Not down