agent-first-psql 0.7.1

A PostgreSQL interface for AI agents: reliable, structured, explicit, and read-only by default.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<!-- Generated. Do not edit by hand. Regenerate: afpsql --help --recursive --output markdown -->

# afpsql CLI Reference

# afpsql - A PostgreSQL interface for AI agents: reliable, structured, explicit, and read-only by default.

`afpsql` gives agents a reliable PostgreSQL contract: structured stdout
events, first-class SSH/container transports, explicit write permissions,
stable pipe sessions, and machine-readable failures.

### Interface Policy

- default mode is canonical agent-first CLI
- `--mode psql` is argument translation only; runtime output stays JSONL
- stdout carries protocol events; stderr is not a protocol channel
- native CLI and pipe mode default to read-only transactions; writes require permission
- SSH/container transports keep afpsql local instead of running human `psql` across boundaries

### Modes

- default (native CLI): one SQL action per process — a single agent step
- `--mode pipe`: a long-lived JSONL session with `id` correlation and named sessions for multi-step work
- `--mode psql`: run existing `psql` scripts unchanged — flags are translated, runtime output stays JSONL

### Query Sources and Parameters

- use `--sql` for inline SQL or `--sql-file` for a file
- use repeatable `--param N=value` for positional binds
- placeholder count is validated from prepared-statement metadata, not by SQL text scanning

### Connection Sources

- `--dsn-secret` for a PostgreSQL URI
- `--conninfo-secret` for libpq-style conninfo
- or discrete `--host`, `--port`, `--user`, `--dbname`, `--password-secret`
- every `*-secret` flag has a `*-secret-env` partner that reads the value from a named environment variable
- every secret slot also has a `*-secret-config FILE DOT_PATH` source for JSON, TOML, YAML, or dotenv
- add `--ssh user@server` when PostgreSQL is reachable only from the server boundary
- add `--container TARGET` when PostgreSQL is reachable only from inside a container boundary
- use named container scope flags instead of raw driver option passthrough
- use `--container-driver docker|podman|nerdctl|compose|kubectl` for the exec syntax
- combine `--ssh user@server --container TARGET` for containers on an SSH host
- agent-first environment fallbacks: `AFPSQL_*`
- PostgreSQL environment fallbacks: `PGHOST`, `PGPORT`, `PGUSER`, `PGDATABASE`, `PGPASSWORD`, `PGSSLMODE`

### Result Shaping

- default mode buffers a bounded inline result
- use `--stream-rows` for large result sets, with `--batch-rows` and `--batch-bytes` to tune chunk size
- `--output json|yaml|plain` changes rendering only, not the runtime schema

### Examples

```text
afpsql --sql "select now() as now_rfc3339"
afpsql --sql-file ./query.sql
afpsql --sql 'select * from users where id = $1' --param 1=123
afpsql --dsn-secret-env DATABASE_URL --sql "select 1"
afpsql --dsn-secret-config config.yaml database.url --sql "select 1"
afpsql --ssh user@server --host 127.0.0.1 --port 5432 --user app --dbname appdb --sql "select 1"
afpsql --container pg-container --dsn-secret-env DATABASE_URL --sql "select 1"
afpsql --ssh root@server --container app --host host.container.internal --port 5432 --user app --dbname appdb --sql "select 1"
afpsql --mode psql -h 127.0.0.1 -p 5432 -U app -d appdb -c "select 1"
afpsql --sql "select * from big_table" --stream-rows --batch-rows 1000
afpsql --mode pipe
afpsql psql status
afpsql psql install
afpsql skill status
afpsql skill install
```

### Exit Codes

- `0`: query completed successfully
- `1`: SQL error or runtime error
- `2`: invalid CLI arguments

```text
Usage: afpsql [OPTIONS] [COMMAND]

Commands:
  psql     Manage the local psql wrapper for afpsql --mode psql
  skill    Manage Agent-First PSQL skills for Codex, Claude Code, opencode, and Hermes
  inspect  Schema discovery: inspect databases, schemas, tables, indexes, or snapshots
  help     Print this message or the help of the given subcommand(s)

Options:
  -h, --help
          Print help. Add --recursive to expand every nested subcommand; add --output json|yaml|markdown to render this help in another format.

  -V, --version
          Print version

Query:
      --sql <SQL>
          Inline SQL string to execute

      --sql-file <SQL_FILE>
          Read SQL from a file

      --param <PARAM>
          Positional bind parameter in `N=value` form. Repeat for additional parameters

      --stream-rows
          Stream large result sets as `result_rows` batches instead of a single inline result

      --batch-rows <BATCH_ROWS>
          Maximum rows per streamed batch

      --batch-bytes <BATCH_BYTES>
          Soft byte target per streamed batch

      --statement-timeout-ms <STATEMENT_TIMEOUT_MS>
          Per-query statement timeout in milliseconds

      --lock-timeout-ms <LOCK_TIMEOUT_MS>
          Per-query lock timeout in milliseconds

      --inline-max-rows <INLINE_MAX_ROWS>
          Maximum inline rows before returning `result_too_large`

      --inline-max-bytes <INLINE_MAX_BYTES>
          Maximum inline payload bytes before returning `result_too_large`

      --permission <PERMISSION>
          Query permission. Defaults to read, ssh-read with --ssh, or container-read with --container

          [possible values: read, write, ssh-read, ssh-write, container-read, container-write]

      --dry-run
          Preview the query without executing it

      --explain
          Wrap the query in EXPLAIN (FORMAT JSON) and return the plan tree instead of executing the user's SQL

      --explain-analyze
          Wrap the query in EXPLAIN (ANALYZE, FORMAT JSON, BUFFERS). The underlying SQL actually runs; writes require the matching write permission

Connection:
      --dsn-secret <DSN_SECRET>
          PostgreSQL DSN URI. Redacted in structured output

      --dsn-secret-env <DSN_SECRET_ENV>
          Read PostgreSQL DSN URI from an environment variable

      --dsn-secret-config <FILE> <DOT_PATH>
          Read PostgreSQL DSN URI from FILE at DOT_PATH

      --conninfo-secret <CONNINFO_SECRET>
          libpq-style conninfo string. Redacted in structured output

      --conninfo-secret-env <CONNINFO_SECRET_ENV>
          Read libpq-style conninfo string from an environment variable

      --conninfo-secret-config <FILE> <DOT_PATH>
          Read libpq-style conninfo from FILE at DOT_PATH

      --host <HOST>
          PostgreSQL host

      --port <PORT>
          PostgreSQL port

      --user <USER>
          PostgreSQL user name

      --dbname <DBNAME>
          PostgreSQL database name

      --password-secret <PASSWORD_SECRET>
          PostgreSQL password. Redacted in structured output

      --password-secret-env <PASSWORD_SECRET_ENV>
          Read PostgreSQL password from an environment variable

      --password-secret-config <FILE> <DOT_PATH>
          Read PostgreSQL password from FILE at DOT_PATH

SSH Transport:
      --ssh <SSH>
          Open an SSH transport to USER@HOST before connecting to PostgreSQL

      --ssh-via <SSH_VIA>
          SSH hop to reach before the final --ssh destination. Repeat for multiple hops

      --ssh-option <SSH_OPTIONS>
          Additional OpenSSH -o option. Repeat for multiple options

      --ssh-local-host <SSH_LOCAL_HOST>
          Local bind host for the SSH tunnel

      --ssh-local-port <SSH_LOCAL_PORT>
          Local bind port for the SSH tunnel. Defaults to an ephemeral port

      --ssh-remote-socket <SSH_REMOTE_SOCKET>
          Explicit remote PostgreSQL Unix socket path for SSH forwarding

      --ssh-sudo-user <SSH_SUDO_USER>
          Remote OS user for sudo -n Unix-socket bridge mode; requires an explicit socket

Container Transport:
      --container <CONTAINER>
          Run a container exec stdio bridge in TARGET before connecting to PostgreSQL

      --container-driver <CONTAINER_DRIVER>
          Container exec driver: docker, podman, nerdctl, compose, or kubectl

      --container-runtime <CONTAINER_RUNTIME>
          Runtime command for the selected container driver. Defaults to the driver command

      --container-user <CONTAINER_USER>
          OS user passed to drivers that support exec user selection

      --container-namespace <CONTAINER_NAMESPACE>
          Kubernetes namespace for kubectl exec

      --container-context <CONTAINER_CONTEXT>
          Docker or Kubernetes context for the selected driver

      --container-compose-file <CONTAINER_COMPOSE_FILES>
          Compose file passed before compose exec. Repeat for multiple files

      --container-compose-project <CONTAINER_COMPOSE_PROJECT>
          Compose project name passed before compose exec

      --container-pod-container <CONTAINER_POD_CONTAINER>
          Kubernetes container name for multi-container pods

Runtime:
  -o, --output <OUTPUT>
          Output format: json (default), yaml, or plain

          [default: json]

      --stdout-file <PATH>
          Redirect stdout bytes to this file

      --stderr-file <PATH>
          Redirect stderr bytes to this file

      --log <LOG>
          Diagnostic log categories (comma-separated). Categories: startup, connect, query, transport, mode; or an exact event name like `query.error`; or `all` for everything

      --mode <MODE>
          Runtime mode: canonical cli, pipe, or `psql` translation mode

          [default: cli]
          [possible values: cli, pipe, psql]
```

## afpsql psql - Manage the local psql wrapper for afpsql --mode psql

```text
Usage: psql <COMMAND>

Commands:
  status     Show whether the afpsql-managed psql wrapper is installed and active
  install    Install an afpsql-managed psql wrapper
  uninstall  Remove an afpsql-managed psql wrapper
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help
          Print help
```

### afpsql psql status - Show whether the afpsql-managed psql wrapper is installed and active

```text
Usage: status [OPTIONS]

Options:
      --bin-dir <BIN_DIR>
          Directory that contains the psql wrapper. Defaults to the afpsql executable directory

  -h, --help
          Print help
```

### afpsql psql install - Install an afpsql-managed psql wrapper

```text
Usage: install [OPTIONS]

Options:
      --bin-dir <BIN_DIR>
          Directory that contains the psql wrapper. Defaults to the afpsql executable directory

  -h, --help
          Print help
```

### afpsql psql uninstall - Remove an afpsql-managed psql wrapper

```text
Usage: uninstall [OPTIONS]

Options:
      --bin-dir <BIN_DIR>
          Directory that contains the psql wrapper. Defaults to the afpsql executable directory

  -h, --help
          Print help
```

## afpsql skill - Manage Agent-First PSQL skills for Codex, Claude Code, opencode, and Hermes

```text
Usage: skill <COMMAND>

Commands:
  status     Show whether the Agent-First PSQL skill is installed, valid, and up to date
  install    Install the Agent-First PSQL skill
  uninstall  Remove an afpsql-managed Agent-First PSQL skill
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help
          Print help
```

### afpsql skill status - Show whether the Agent-First PSQL skill is installed, valid, and up to date

```text
Usage: status [OPTIONS]

Options:
      --agent <AGENT>
          Agent to manage. Defaults to all personal skill targets

          Possible values:
          - all:         Manage every agent that supports the requested scope
          - codex:       Manage the Codex local skill under $CODEX_HOME/skills
          - claude-code: Manage the Claude Code skill under ~/.claude/skills or .claude/skills
          - opencode:    Manage the opencode skill under ~/.config/opencode/skills or .opencode/skills
          - hermes:      Manage the Hermes skill under $HERMES_HOME/skills or ~/.hermes/skills

          [default: all]

      --scope <SCOPE>
          Skill scope

          Possible values:
          - personal:  Install under the user-level skills directory
          - workspace: Install under the current workspace's skills directory

          [default: personal]

      --skills-dir <SKILLS_DIR>
          Directory that contains skill folders. Requires an explicit single --agent

  -h, --help
          Print help (see a summary with '-h')
```

### afpsql skill install - Install the Agent-First PSQL skill

```text
Usage: install [OPTIONS]

Options:
      --agent <AGENT>
          Agent to manage. Defaults to all personal skill targets

          Possible values:
          - all:         Manage every agent that supports the requested scope
          - codex:       Manage the Codex local skill under $CODEX_HOME/skills
          - claude-code: Manage the Claude Code skill under ~/.claude/skills or .claude/skills
          - opencode:    Manage the opencode skill under ~/.config/opencode/skills or .opencode/skills
          - hermes:      Manage the Hermes skill under $HERMES_HOME/skills or ~/.hermes/skills

          [default: all]

      --scope <SCOPE>
          Skill scope

          Possible values:
          - personal:  Install under the user-level skills directory
          - workspace: Install under the current workspace's skills directory

          [default: personal]

      --skills-dir <SKILLS_DIR>
          Directory that contains skill folders. Requires an explicit single --agent

      --force
          Overwrite or remove an unmanaged Agent-First PSQL skill at the target path

  -h, --help
          Print help (see a summary with '-h')
```

### afpsql skill uninstall - Remove an afpsql-managed Agent-First PSQL skill

```text
Usage: uninstall [OPTIONS]

Options:
      --agent <AGENT>
          Agent to manage. Defaults to all personal skill targets

          Possible values:
          - all:         Manage every agent that supports the requested scope
          - codex:       Manage the Codex local skill under $CODEX_HOME/skills
          - claude-code: Manage the Claude Code skill under ~/.claude/skills or .claude/skills
          - opencode:    Manage the opencode skill under ~/.config/opencode/skills or .opencode/skills
          - hermes:      Manage the Hermes skill under $HERMES_HOME/skills or ~/.hermes/skills

          [default: all]

      --scope <SCOPE>
          Skill scope

          Possible values:
          - personal:  Install under the user-level skills directory
          - workspace: Install under the current workspace's skills directory

          [default: personal]

      --skills-dir <SKILLS_DIR>
          Directory that contains skill folders. Requires an explicit single --agent

      --force
          Overwrite or remove an unmanaged Agent-First PSQL skill at the target path

  -h, --help
          Print help (see a summary with '-h')
```

## afpsql inspect - Schema discovery: inspect databases, schemas, tables, indexes, or snapshots

```text
Usage: inspect <COMMAND>

Commands:
  databases  List databases on the connected server with size, encoding, and connection facts
  database   Summarize the connected database: schema/table/view/sequence counts and size
  schemas    List user-visible schemas
  schema     Export full schema metadata for one schema
  snapshot   Export a stable full-schema snapshot for machine consumption
  tables     List tables in a schema with owner, estimated rows, and size
  views      List views (regular and materialized) in a schema with owner
  indexes    List indexes with definitions, size, validity, and optional usage stats
  table      Describe a table's columns: types, nullability, defaults, primary key, comments
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help
          Print help
```

### afpsql inspect databases - List databases on the connected server with size, encoding, and connection facts

```text
Usage: databases [OPTIONS]

Options:
      --all
          Include template databases (template0/template1) in the listing

  -h, --help
          Print help
```

### afpsql inspect database - Summarize the connected database: schema/table/view/sequence counts and size

```text
Usage: database

Options:
  -h, --help
          Print help
```

### afpsql inspect schemas - List user-visible schemas

```text
Usage: schemas

Options:
  -h, --help
          Print help
```

### afpsql inspect schema - Export full schema metadata for one schema

```text
Usage: schema [OPTIONS]

Options:
      --schema <SCHEMA>
          Schema to inspect. Defaults to `public`

          [default: public]

      --like <LIKE>
          Optional `LIKE` pattern matched against relation names (use `%` as wildcard)

  -h, --help
          Print help
```

### afpsql inspect snapshot - Export a stable full-schema snapshot for machine consumption

```text
Usage: snapshot [OPTIONS]

Options:
      --schema <SCHEMA>
          Schema to inspect. Defaults to `public`

          [default: public]

      --like <LIKE>
          Optional `LIKE` pattern matched against relation names (use `%` as wildcard)

  -h, --help
          Print help
```

### afpsql inspect tables - List tables in a schema with owner, estimated rows, and size

```text
Usage: tables [OPTIONS]

Options:
      --schema <SCHEMA>
          Schema to filter on. Defaults to `public`

          [default: public]

      --like <LIKE>
          Optional `LIKE` pattern matched against the table name (use `%` as wildcard)

  -h, --help
          Print help
```

### afpsql inspect views - List views (regular and materialized) in a schema with owner

```text
Usage: views [OPTIONS]

Options:
      --schema <SCHEMA>
          Schema to filter on. Defaults to `public`

          [default: public]

      --like <LIKE>
          Optional `LIKE` pattern matched against the view name (use `%` as wildcard)

  -h, --help
          Print help
```

### afpsql inspect indexes - List indexes with definitions, size, validity, and optional usage stats

```text
Usage: indexes [OPTIONS]

Options:
      --schema <SCHEMA>
          Schema to filter on. Defaults to `public`

          [default: public]

      --table <TABLE>
          Optional table name to filter on. Accepts `schema.table` to override --schema

      --stats
          Include PostgreSQL's built-in pg_stat_user_indexes usage counters

  -h, --help
          Print help
```

### afpsql inspect table - Describe a table's columns: types, nullability, defaults, primary key, comments

```text
Usage: table [OPTIONS] <NAME>

Arguments:
  <NAME>
          Table name. Accepts `schema.table`; defaults to `public.NAME` when unqualified

Options:
      --full
          Include relation, constraints, indexes, triggers, and sequence/default metadata

  -h, --help
          Print help
```
AFDATA: 0.19.1