Skip to content
Lobste.rs Databases· pgstack.io by exAspArk·· 2d ago

Giving AI Agents Safe Access to PostgreSQL: Read-Only Roles, Temporary Credentials and Query Logging

AI Agents Shouldn't Have Permanent Database Credentials

Key Takeaway

Restricting an AI agent to a read-only PostgreSQL role, read-only transactions or a read replica limits writes, but permissions are additive and PUBLIC grants can still allow updates.

Why It Matters

Adopting this requires automating credential provisioning and cleanup, and explicitly terminating existing sessions when access ends, since expired passwords do not close open connections.

Technical Breakdown · Distilled from the original source

Read-only access options and limits

  • Read-only transactions: BEGIN READ ONLY; per transaction, or ALTER ROLE agent SET default_transaction_read_only = on; as default. An agent with write privileges can override with SET default_transaction_read_only = off;, so this is only an extra precaution.
  • Read-only roles: create agent_readonly NOLOGIN, grant CONNECT, USAGE ON SCHEMA public and SELECT ON public.orders, then a login role inheriting them. PostgreSQL enforces these permissions regardless of transaction mode.
  • Additive permissions: if UPDATE is granted to PUBLIC, the read-only user can still modify orders.
  • Read replicas: a standby rejects writes even if the role has write permissions, and moves CPU/memory load off the primary. Caveat: replica lag can show stale state.

Temporary credentials and query logging

  • ALTER ROLE agent_142 VALID UNTIL '2026-12-01 12:15:00+00'; rejects new connections with the expired password, but existing connections stay open and queries keep succeeding.
  • Full revocation needs ALTER ROLE agent_142 NOLOGIN;, SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = 'agent_142';, then DROP ROLE. Production use requires automated provisioning, scheduled cleanup, retries and verification.
  • ALTER ROLE agent_142 SET log_statement = 'all'; must be set before the agent connects; per-investigation login roles map log entries to investigations. Limits: statements with syntax errors may be omitted and sensitive values may appear; a full audit trail needs external log storage, recorded justification, searchable review and retention control.

Proxy approach and selection

  • A PostgreSQL-compatible proxy can issue virtual credentials, bind them to a session, forward queries as a restricted upstream user, record SQL and end sessions on schedule; PgStack Access returns a temporary URL via pgstack access start --env production --duration 15m --read-only and invalidates it after 15 minutes (the author works on PgStack).
  • Costs: extra infrastructure, availability, connection management and protocol feature support; encrypted connections, encrypted credentials and recordings, restricted access to recordings; underlying permissions still matter.
  • Selection: no live data needs a dev or sanitized copy; occasional production access needs a restricted role with expiring password and query logging; team-wide access needs centralized access and query recording.
Source: Lobste.rs Databases· pgstack.io

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference