PostgreSQL Field Guide
AI / Agent referenceAI / agent reference

AI / agent reference

Let models use PostgreSQL under explicit schema, privilege, cost, and failure contracts

A model does not understand your database merely because it can write SQL. Reliable systems turn database context into a contract, narrow execution into tools, and make correctness repeatably testable.

user intent
  → task class (read / write / DDL / operations)
  → retrieve schema contract and relevant guidance
  → model emits a structured tool call
  → policy layer checks AST, privilege, cost, and parameters
  → restricted database role executes
  → return row count, SQLSTATE, duration, and truncation state
  → write an audit event

Database credentials do not enter model context. The model does not choose connection targets. The tool binds environment, database, schema, and role.

Risk tiers

TierExampleDefault policy
R0List/describe schema, bounded readAuto-run with a short timeout
R1Sensitive columns, larger aggregatePermission filter, audit, cost bound
R2INSERT or primary-key single-row UPDATEDry run plus business API or explicit approval
R3Bulk writes, DDL, grants, replication, restoreNot exposed to a general agent; expert workflow

Prompts are not a security boundary

“Do not delete data” is behavioral advice. Real boundaries come from roles, network isolation, read-only transactions, SQL parsing, and tool allowlists.

Minimum bar

A deployable database agent should parameterize all values; default to read-only; bound statement time and result rows; reject multiple statements; never return secrets to the model; audit query fingerprints; and handle SQLSTATE values such as 40001, 40P01, and 57014 deterministically.

Last updated on

On this page