r/postgres • u/RaymondArtin • 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?
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.