pg_policy 0.1.0

This Release
pg_policy 0.1.0
Date
Status
Stable
Abstract
Agentic policy language for PostgreSQL — guardrails, guidance, and session-aware controls
Description
pg_policy adds an Agent Policy Language (APL) to PostgreSQL for AI-agent guardrails, soft guidance, and temporal session limits. It complements row-level security and is designed for tool gateways and MCP brokers that authorize agent actions beside your data.
Released By
pgpapa
License
PostgreSQL
Resources
Special Files
Tags

Extensions

pg_policy 0.1.0
Agentic policy language for PostgreSQL

Documentation

02-postgres-extension-feasibility
PostgreSQL Extension Feasibility: Policy Languages & “Custom Syntax”
07-value-thesis
Why databases must govern agents (and how pg_policy does it)
README
Use-case catalog
01-architecture
Architecture
04-industry-analysis
Industry Analysis: Database Policy for Agentic AI
CONTRIBUTING
Contributing to pg_policy
README
Domain policy packs
01-policy-language-landscape
Policy Language Landscape for Database-Resident Enforcement
packs
Policy packs
05-competitive-positioning
Competitive Positioning & Moat Analysis
001-language-design
ADR-001: Agent Policy Language (APL) Design
language
Agent Policy Language (APL)
SECURITY
Security Policy
integrations
Adapters: make pg_policy work for every agent runtime
003-enforcement-modes
ADR-003: Graduated Enforcement Modes
README
Universal onboarding (works for every stack)
README
Integration snippets
INSTALL
Installing pg_policy
README
Documentation index
roadmap
Roadmap
00-product-decision
Product Decision: pg_policy
feature_request
Feature request
08-capability-backlog
Capability backlog: what else pg_policy should grow
002-syntax-strategy
ADR-002: Syntax Strategy Without Parser Plugins
SUPPORT
Support
PULL_REQUEST_TEMPLATE
PULL_REQUEST_TEMPLATE
03-agentic-ai-guardrails
Agentic AI Guardrails, Guidance, and Database Policy
06-marketplace-playbook
Marketplace & Distribution Playbook
CODE_OF_CONDUCT
Contributor Covenant Code of Conduct

README

pg_policy

Agentic policy language for PostgreSQL — guardrails, guidance, and session-aware controls beside your data.

pg_policy is a PostgreSQL extension that lets you authorize and steer AI agents with a small, readable Agent Policy Language (APL). It complements row-level security: RLS protects rows; pg_policy governs agent tools, sessions, and soft guidance.

CREATE EXTENSION pg_policy;

SELECT pg_policy.upsert_policy('block_ddl', $apl$
forbid
  principal agent "research_bot"
  action tool "execute_sql"
  when { context.statement_type in ["DROP", "TRUNCATE", "ALTER", "CREATE"] }
  reason "Research agents may not run DDL"
$apl$);

SELECT pg_policy.set_setting('enforcement_mode', 'enforce');

SELECT pg_policy.check(
  'research_bot',
  'execute_sql',
  '{"statement_type":"DROP"}'::jsonb
);  -- false

Why this exists

AI agents increasingly hold database credentials and tool access. Classical privileges and RLS answer which rows, but not:

  • May this agent call this tool with these arguments?
  • Has this session already exhausted an export budget?
  • Should we steer the agent toward a safer tool (guidance), not only deny?

Industry systems (Cedar, OPA/Rego, OpenFGA, Dogwood) solve pieces of this outside the database. pg_policy brings an agent-native policy layer into PostgreSQL so policies, session events, decision logs, and data share one trust boundary.

Start here for the argument: Why databases must govern agents · Use cases · Onboard in 30 minutes · Load a policy pack

Thorough research lives in docs/research/.


Features

Capability Description
APL Cedar/Dogwood-inspired permit / forbid / guide documents
Guardrails Hard deny/allow for tools and actions
Guidance Soft obligations (advice, prefer_tool, max_rows)
Temporal limits Session event counts within intervals
Graduated modes log_onlyguideenforce
Decision log Every evaluation audited
RLS complement Recipes that sit beside CREATE POLICY
SQL-only v0.1 No compilers required to try it

Syntax note: PostgreSQL does not allow extensions to add core SQL keywords. APL is additional policy syntax invoked from SQL via dollar-quoting—the portable, PGXN-friendly approach.


Install

From source

git clone https://github.com/rahiakil/pg-policy.git
cd pg-policy
make install
psql -d mydb -c "CREATE EXTENSION pg_policy;"

Requires PostgreSQL 14+ (tested target: 14–17) and a normal PGXS toolchain (pg_config on PATH).

Smoke test

psql -d mydb -f examples/01-basic-guardrails.sql

Quick start

-- Soft onboarding: observe only
SELECT pg_policy.set_setting('enforcement_mode', 'log_only');

SELECT pg_policy.upsert_policy('export_budget', $apl$
forbid
  principal agent "research_bot"
  action tool "export_csv"
  when temporal {
    count(action == "export_csv") within interval '1 hour' >= 3
  }
  reason "Export budget exceeded"
$apl$);

SELECT pg_policy.open_session('sess-1', 'agent', 'research_bot');

SELECT pg_policy.evaluate(
  'agent', 'research_bot', 'tool', 'export_csv',
  '*', '*', '{}'::jsonb, 'sess-1'
);

Promote to enforce after a shadow period:

SELECT pg_policy.set_setting('enforcement_mode', 'enforce');

Architecture (short)

Agent / MCP gateway
        │
        ▼
 pg_policy.evaluate(...)
        │
        ├─ match APL policies (permit/forbid/guide)
        ├─ evaluate context + temporal session events
        ├─ write decision_log (+ optional events)
        └─ return { decision, obligations, reasons }
                │
                ├─ deny  → gateway blocks tool
                ├─ allow → tool runs; SQL still hits RLS
                └─ obligations → steer planner / UX

Design decisions: docs/design/ · ADRs: docs/adr/


Documentation

Doc Contents
APL language Syntax reference
Extension guide Install & concepts
Policy landscape Cedar, Rego, CEL, Zanzibar, Dogwood, RLS, …
Extension feasibility Hooks, PGXS, packaging
Agentic guardrails Guardrail vs guidance
Industry analysis Living market notes
Positioning Category & moat
Marketplace playbook PGXN → managed clouds
Value thesis Why DB + agents need plane B
Capability backlog What to add next
Use cases Analytics, support, fintech, health, …
Onboarding Universal path for every framework
Policy packs Drop-in APL templates
Roadmap Toward PGXN / 1.0

Examples & packs

Tutorials

Domain packs (baseline first, then one domain) — examples/packs/

Python PEPexamples/integrations/evaluate_middleware.py


Status

v0.1.0 — research-backed SQL/PL/pgSQL MVP suitable for experimentation and API feedback. Not yet a hardened production security boundary; use with RLS, least-privilege roles, and a tool gateway. See SECURITY.md.

Roadmap highlights: pgrx-accelerated evaluator, CEL/Cedar condition backends, AuthZEN mapping, PGXN release, managed-provider packaging.


Contributing

See CONTRIBUTING.md. By participating you agree to the Code of Conduct.

License

pg_policy is released under the PostgreSQL License — the same style of license used across the PostgreSQL ecosystem.

  • Repository: https://github.com/rahiakil/pg-policy
  • Issues: https://github.com/rahiakil/pg-policy/issues
  • PGXN: planned (see roadmap)