AI coding agents that generate SQL on the fly can rewrite your database without a human ever seeing the statements.
Why audit trail matters for AI coding agents
When a language model writes queries, the intent is often hidden behind a prompt. The resulting statements may create tables, delete rows, or exfiltrate data. Without a clear audit trail, you cannot answer who caused a change, what data was accessed, or whether the operation complied with policy. Auditors, incident responders, and compliance teams all rely on immutable logs that tie each command to a verifiable identity.
The gaps in traditional database logging
PostgreSQL’s native logs capture query text and timestamps, but they lack context. The logs do not record the original requester’s identity when a service account or an AI agent uses a shared credential. Masking of sensitive columns is impossible at the logging layer, so logs may expose personal data. Moreover, native logs cannot enforce approvals or block dangerous commands before they reach the engine. The result is an audit trail that is incomplete, noisy, and potentially non‑compliant.
A gateway approach to a reliable audit trail
Placing a layer‑7 gateway between the AI agent and PostgreSQL solves these gaps. The gateway intercepts every protocol message, records the full session, and attaches the authenticated user or service identity to each statement. It can mask sensitive fields in responses, enforce just‑in‑time approvals for high‑risk commands, and block destructive operations before they execute. Because the gateway sits on the data path, the audit trail it creates is authoritative and cannot be bypassed by the agent.
In practice, you deploy the gateway as a Docker Compose service or in Kubernetes, configure a PostgreSQL connection, and let the AI agent connect through the gateway using its standard client libraries. Identity is supplied via OIDC or SAML, so the gateway knows exactly which user or service account initiated each request. The result is a single source of truth for who did what, when, and with what data.
Key enforcement outcomes
- Session recording – every interaction is stored for replay and forensic analysis.
- Inline masking – sensitive columns are redacted in real time, protecting downstream logs.
- Just‑in‑time access – permissions are granted only for the duration of a specific request.
- Command‑level approval – high‑risk statements trigger an approval workflow before they run.
- Identity‑aware audit trail – each query is tied to the originating identity, not a shared credential.
These outcomes exist only because the gateway is the enforcement point. Without it, the underlying PostgreSQL instance would continue to operate with the same blind spots.
