PostgreSQL Setup
Prepare PostgreSQL for AgentShield. Migrations, the agentshield_app role, row level security, connection settings, and upgrades.
PostgreSQL is AgentShield's source of truth. Every decision, approval, execution, and audit link is committed there, and the gateway will not authorize without it.
Requirements
- PostgreSQL 16 (the version used in development and CI).
pgcrypto-stylegen_random_uuid()support (built in since PostgreSQL 13).- TLS for any non-local connection (
sslmode=requireor stricter inDATABASE_URL).
Apply migrations
infrastructure/docker/postgres/migrations/ is the schema source of truth. Apply every file in lexical order as the schema owner:
for f in infrastructure/docker/postgres/migrations/0*.sql; do
psql "$OWNER_DATABASE_URL" -v ON_ERROR_STOP=1 -f "$f"
done| Migration | Purpose |
|---|---|
0001_init.sql | Core tables, constraints, triggers, the audit chain |
0002_user_sessions.sql | Human sessions |
0003_chaos_scenarios.sql | Chaos scenario catalog |
0004_app_role_rls.sql | agentshield_app, RLS policies, SECURITY DEFINER helpers, audit_head_exports |
0005_invitations.sql | Invitations and forced password change |
0006_event_pipeline.sql | Outbox trigger, evidence records, relay functions, reliability uniqueness |
Each file is safe to run twice. schema_migrations records what has been applied. On Kubernetes, the same files are packaged by a kustomization next to the migrations, and a repository test fails if one is missing.
No service creates or alters tables at runtime.
The application role
0004_app_role_rls.sql creates agentshield_app. Set its password and use it in every service's DATABASE_URL:
ALTER ROLE agentshield_app WITH PASSWORD '<strong password>';postgres://agentshield_app:<password>@db.internal:5432/agentshield?sslmode=requireWith this role:
- Row level security filters tenant tables by
app.organization_id, which each service sets per transaction after authentication. - Cross-organization operations (agent lookup, sign-in, session resolution, invitations, relay claim and acknowledge) go through narrow
SECURITY DEFINERfunctions. - The role has no direct access to
event_outboxorreliability_cursor.
AGENTSHIELD_ENV=production refuses to start the gateway, control plane, or relay with any other database user.
Keep the owner for migrations only
The schema owner bypasses RLS and can disable the append-only triggers. Use it to apply migrations and to run the bootstrap command, then keep its credentials out of every service.
Bootstrap the first owner
Run once, with the owner's connection, outside production mode:
DATABASE_URL="$OWNER_DATABASE_URL" \
AGENTSHIELD_BOOTSTRAP_EMAIL=security-lead@example.com \
AGENTSHIELD_BOOTSTRAP_PASSWORD='<12+ characters>' \
AGENTSHIELD_BOOTSTRAP_DISPLAY_NAME='Security Lead' \
AGENTSHIELD_BOOTSTRAP_ORG_NAME='Example Corp' \
AGENTSHIELD_BOOTSTRAP_ORG_SLUG=example-corp \
npm run bootstrap --workspace @agentshield/control-planeThe organization slug must be lowercase alphanumeric segments separated by hyphens. Sign in and change the password after first use.
Sizing and maintenance
- Write volume. Each authorize writes an invocation, decision, event, and audit link; each execute updates the execution and appends another event. Size I/O for roughly two transactions per tool call.
- Audit chain contention. Appends for one organization are serialized by design. Very high per-organization rates contend on the chain head.
- Growth. Event, decision, and chain tables are append-only and grow without bound. Plan storage and archival; never delete rows from append-only tables.
- Connections. Each service uses a connection pool. Account for replicas when setting
max_connections.
Upgrades
- Back up the database.
- Apply new migration files in order with the owner role.
- Roll out new service images.
Migrations are written to be applied before the services that need them.
Implementation references
Was this page helpful?