# postgres.query.run

Runs SQL on a PostgreSQL database

# postgres.query.run

```
postgres.query.run --credential TEXT [--query TEXT] [--param KEY=EXPR…] [--write] [--limit N] → query
```

Runs SQL on a PostgreSQL database. The database is named by `--credential <credential id>` (from `credential_list`, kind postgres — an id, not a connection slug). SQL goes in the body after `--` (or `--query`); values bind as `$1, $2, …` from `--param 1=… --param 2=…` — a literal is text, a whole `{{ expr }}` keeps its type. The statement runs in a read-only transaction unless `--write` is set, so an INSERT/UPDATE/DELETE/DDL without it is refused. Adds `query: { rows, columns, row_count, truncated }` (`input.query.rows[0].id`), plus `rows_affected` with `--write`; add `RETURNING` to get rows back from a write. `--limit` caps the rows (default 200, at most 5000). Never build SQL text from input; bind it. Inside a DSL body write `concat()` rather than `||`, the parser reads `|` as a node separator.

## Flags

- `--credential` (required): Credential id of the PostgreSQL connection (from credential_list, kind postgres).
- `--query`: The SQL statement (or the body after `--`). A literal or {{ expr }}.
- `--param`: One bind value, repeated: `1=…` binds $1, `2=…` binds $2. A literal is text; a whole {{ expr }} keeps its JSON type, so a number stays a number.
- `--write`: Let the statement change data. Without it the query runs read-only and a write is refused. Literal; never {{ }}.
- `--limit`: Rows answered (default 200, at most 5000); `truncated` says more matched.

## Examples

### Read with a bound value

```
| postgres.query.run --credential pg_main --param "1={{ $trigger.auth.sub }}" -- "SELECT id, email FROM accounts WHERE id = $1"
```

It answers:

```json
{
  "query": {
    "rows": [
      {
        "id": 7,
        "email": "a@example.com"
      }
    ],
    "columns": [
      "id",
      "email"
    ],
    "row_count": 1,
    "truncated": false
  }
}
```

### Insert and get the id back

```
| postgres.query.run --credential pg_main --write --param "1={{ $trigger.body.email }}" -- "INSERT INTO accounts (email) VALUES ($1) RETURNING id"
```

It answers:

```json
{
  "query": {
    "rows": [
      {
        "id": 8
      }
    ],
    "columns": [
      "id"
    ],
    "row_count": 1,
    "truncated": false,
    "rows_affected": 1
  }
}
```

## Used in

- [Let an app build inside its own project (control door)](/examples/app-control-door.md)
- [Auth and Authorization](/examples/auth-and-authorization.md)
- [Cookie + JWT Authentication](/examples/cookie-jwt-auth.md)
- [Static Entry Generation](/examples/static-entry-generation.md)
- [Webhook → TSX Page](/examples/webhook-page-tsx.md)
- [REST API + PostgreSQL](/examples/webhook-restapi-postgres.md)
