# PostgreSQL - Query

> Execute SQL queries on PostgreSQL databases.

Source: https://docs.sommatic.ai/en/nodes/integrations.postgresql.query/

- Slug: `integrations.postgresql.query`
- Family: Integrations
- Version: 1.0.0
- Status: Available
- Execution: async

Execute SQL queries on PostgreSQL databases.

Connect your PostgreSQL instance and run parameterized queries with $1, $2 positional binding for SQL injection prevention. Compatible with PostgreSQL, CockroachDB, Supabase, and Neon.

**Operations:**
- **SELECT:** Fetch rows from tables with filters, joins, and aggregations. Returns empty port when no results.
- **INSERT:** Add new records to tables with RETURNING support.
- **UPDATE:** Modify existing records matching a condition.
- **DELETE:** Remove records from tables.
- **Functions:** Call PL/pgSQL functions and stored procedures.
- **DDL:** Execute CREATE, ALTER, DROP statements for schema management.

## Configuration

- `connection_id` (string, required): Credential for your PostgreSQL instance. Accepts credentials of type `connection-string` (postgres:// URI) or `basic-auth` (username + password + host + database).
- `sql` (string, required, accepts expressions): The SQL statement to execute (e.g., "SELECT * FROM users WHERE id = $1").
- `parameters` (array, optional): Array of values for positional $1, $2, ... parameter binding.

## Output ports

- `out` (default): Resulting rows from the query
- `empty`: No rows returned
- `error`: Error occurred during execution

## Input contract

- `connection_id` (string, required)
- `sql` (string, required)
- `parameters` (array, optional)

## Output contract

- `result` (array<object>, optional): Array of rows returned by the query

## In expressions

What a later node can read from this node's output with `$("PostgreSQL - Query").item.json.<path>` (write your node's label instead of `PostgreSQL - Query`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("PostgreSQL - Query").item.json.result` (array) = `[…] (1)`: Array of rows returned by the query


## How it works

Runs one SQL statement on PostgreSQL with positional `$1, $2…` parameters and returns the rows under `result`, or routes to `empty` when no rows come back.

1. Resolves the expressions of the node config. Empty `connection_id` and `sql` fall back to the incoming item. Parameters are read from `params`, then `parameters`, then the incoming item's `params` or `parameters`.
2. Resolves the connection: a `postgres://` / `postgresql://` URI in `connection_id`, a `connection-string` credential, or a `basic-auth` / `object` credential with `username`, `password`, `host`, `database` and `port` (default 5432).
3. Connects, runs `sql` with the parameters (a single non-array value is wrapped in an array) and closes the connection.
4. Returns `{ result: rows }` on `out`, or `{ result: [] }` on `empty` when no rows are returned.

## In a workflow

### Node

```json
{
  "node_id": "node-find-customer",
  "label": "Find customer",
  "operator_slug": "integrations.postgresql.query",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "sql": "SELECT id, email, created_at FROM customers WHERE email = $1",
    "parameters": [
      "{{ $json.email }}"
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": [
      {
        "id": 42,
        "email": "ana@example.com",
        "created_at": "2026-11-20T15:04:05.000Z"
      }
    ]
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The statement returned at least one row.

```json
{
  "result": [
    {
      "id": 42,
      "email": "ana@example.com",
      "created_at": "2026-11-20T15:04:05.000Z"
    }
  ]
}
```

### `empty`

The statement returned no rows. This includes INSERT, UPDATE and DELETE without `RETURNING`.

```json
{
  "result": []
}
```

### `error`

Validation, credential resolution, the connection or the statement failed.

```json
{
  "error": "relation \"customers\" does not exist"
}
```

## Behaviour in the code

- `params`: Undeclared alias of `parameters` that takes precedence when both are set.
- `connection_id`: With a connection string, TLS is used without certificate verification except for localhost (no TLS). With a structured credential, TLS is used only when the credential config sets `ssl`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No credential in the config or in the incoming item. |
| `sql query is required` | `error` | `sql` is empty. |
| `bind message supplies <n> parameters, but prepared statement "" requires <m>` | `error` | The number of parameters does not match the `$n` placeholders in `sql`. |
| `Credential <connection_id> has unsupported type '<type>' for integrations.postgresql.query. Use 'connection-string' or 'basic-auth'.` | `error` | The credential type is not supported by this node. |

## Notes and limits

- Use `RETURNING` on writes if you need `out`; otherwise they always route to `empty`.
- A new connection is opened and closed on every run; the node does not retry.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "integrations.postgresql.query",
  "name": "PostgreSQL - Query",
  "category": "integrations",
  "version": "1.0.0",
  "description": "Execute SQL queries on PostgreSQL databases.\n\n  Connect your PostgreSQL instance and run parameterized queries with $1, $2 positional binding for SQL injection prevention. Compatible with PostgreSQL, CockroachDB, Supabase, and Neon.\n\n  **Operations:**\n  - **SELECT:** Fetch rows from tables with filters, joins, and aggregations. Returns empty port when no results.\n  - **INSERT:** Add new records to tables with RETURNING support.\n  - **UPDATE:** Modify existing records matching a condition.\n  - **DELETE:** Remove records from tables.\n  - **Functions:** Call PL/pgSQL functions and stored procedures.\n  - **DDL:** Execute CREATE, ALTER, DROP statements for schema management.",
  "capabilities": [
    "sql",
    "database",
    "postgres",
    "postgresql"
  ],
  "group": [
    {
      "key": "postgresql",
      "name": "PostgreSQL",
      "icon_url": "/icons/integrations/postgresql/postgresql.svg"
    }
  ],
  "icon_url": "/icons/integrations/postgresql/postgresql.svg",
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "PostgreSQL Credential",
        "description": "Credential for your PostgreSQL instance. Accepts credentials of type `connection-string` (postgres:// URI) or `basic-auth` (username + password + host + database).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "connection-string",
            "basic-auth"
          ],
          "types": [
            "postgres"
          ]
        }
      },
      "sql": {
        "type": "string",
        "title": "SQL Query",
        "description": "The SQL statement to execute (e.g., \"SELECT * FROM users WHERE id = $1\").",
        "format": "expression",
        "ui_widget": "sql_editor"
      },
      "parameters": {
        "type": "array",
        "title": "Query Parameters",
        "description": "Array of values for positional $1, $2, ... parameter binding."
      }
    },
    "required": [
      "connection_id",
      "sql"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "sql": {
        "type": "string"
      },
      "parameters": {
        "type": "array"
      }
    },
    "required": [
      "connection_id",
      "sql"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "array",
        "items": {
          "type": "object"
        },
        "description": "Array of rows returned by the query"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Rows",
      "description": "Resulting rows from the query",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "empty",
      "label": "Empty",
      "description": "No rows returned",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Error occurred during execution",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "db-io"
  ],
  "tags": [
    "sql",
    "database"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://sommatic.ai",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "PostgreSQL Credential",
      "description": "Credential for your PostgreSQL instance. Accepts credentials of type `connection-string` (postgres:// URI) or `basic-auth` (username + password + host + database).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "connection-string",
          "basic-auth"
        ],
        "types": [
          "postgres"
        ]
      }
    },
    "sql": {
      "type": "string",
      "title": "SQL Query",
      "description": "The SQL statement to execute (e.g., \"SELECT * FROM users WHERE id = $1\").",
      "format": "expression",
      "ui_widget": "sql_editor"
    },
    "parameters": {
      "type": "array",
      "title": "Query Parameters",
      "description": "Array of values for positional $1, $2, ... parameter binding."
    }
  },
  "required": [
    "connection_id",
    "sql"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "sql": {
      "type": "string"
    },
    "parameters": {
      "type": "array"
    }
  },
  "required": [
    "connection_id",
    "sql"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "array",
      "items": {
        "type": "object"
      },
      "description": "Array of rows returned by the query"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Rows",
    "description": "Resulting rows from the query",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "empty",
    "label": "Empty",
    "description": "No rows returned",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Error occurred during execution",
    "is_default": false,
    "supports_condition": false
  }
]
```
