# PostgreSQL (/integrations/postgresql)

Use [PostgreSQL](https://www.postgresql.org/) to query and modify rows, execute SQL, and inspect tables, columns, keys, and indexes. Schema inspection lets a workflow discover the database structure before generating a query.

### Connection setup \[#connection-setup]

In **Integrations**, add a connection with the host, port, database, username, password, and transport-security settings. Sim proves the connection with `SELECT 1` before saving it encrypted, then reads it on the server for each run; the block, the workflow, and agents never see the password. Reconnect the connection from Integrations when its credentials change. The runtime must be able to reach the database host.

- **Preferred** encrypts when the server offers TLS and otherwise connects without it. An attacker on the network can force that fallback, so choose **Required** when the server supports TLS.
- **Required** always encrypts but does not verify the server certificate, like libpq's `sslmode=require`.
- **Disabled** never encrypts. Use it only on a network whose confidentiality you control.

Workflows built before saved connections keep their inline connection fields and run unchanged.

## Use the block \[#usage-instructions]

Query or modify PostgreSQL rows, execute SQL, and inspect tables, columns, keys, and indexes.

## Actions \[#actions]

### PostgreSQL Query \[#postgresql-query]

Execute a SELECT query on PostgreSQL database

#### Input \[#input]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `query` | string | Yes | SQL SELECT query to execute |

#### Output \[#output]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `rows` | array | Array of rows returned from the query |
| `rowCount` | number | Number of rows returned |

### PostgreSQL Insert \[#postgresql-insert]

Insert data into PostgreSQL database

#### Input \[#input-1]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `table` | string | Yes | Table name to insert data into |
| `data` | object | Yes | Data object to insert (key-value pairs) |

#### Output \[#output-1]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `rows` | array | Inserted data (if RETURNING clause used) |
| `rowCount` | number | Number of rows inserted |

### PostgreSQL Update \[#postgresql-update]

Update data in PostgreSQL database

#### Input \[#input-2]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `table` | string | Yes | Table name to update data in |
| `data` | object | Yes | Data object with fields to update (key-value pairs) |
| `where` | string | Yes | WHERE clause condition (without WHERE keyword) |

#### Output \[#output-2]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `rows` | array | Updated data (if RETURNING clause used) |
| `rowCount` | number | Number of rows updated |

### PostgreSQL Delete \[#postgresql-delete]

Delete data from PostgreSQL database

#### Input \[#input-3]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `table` | string | Yes | Table name to delete data from |
| `where` | string | Yes | WHERE clause condition (without WHERE keyword) |

#### Output \[#output-3]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `rows` | array | Deleted data (if RETURNING clause used) |
| `rowCount` | number | Number of rows deleted |

### PostgreSQL Execute \[#postgresql-execute]

Execute raw SQL query on PostgreSQL database

#### Input \[#input-4]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `query` | string | Yes | Raw SQL query to execute |

#### Output \[#output-4]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `rows` | array | Array of rows returned from the query |
| `rowCount` | number | Number of rows affected |

### PostgreSQL Introspect \[#postgresql-introspect]

Introspect PostgreSQL database schema to retrieve table structures, columns, and relationships

#### Input \[#input-5]

| Parameter | Type | Required | Description |
| - | - | - | - |
| `schema` | string | No | Schema to introspect (default: public) |

#### Output \[#output-5]

| Parameter | Type | Description |
| - | - | - |
| `message` | string | Operation status message |
| `tables` | array | Array of table schemas with columns, keys, and indexes |
| ↳ `name` | string | Table name |
| ↳ `schema` | string | Schema name (e.g., public) |
| ↳ `columns` | array | Table columns |
| ↳ `name` | string | Column name |
| ↳ `type` | string | Data type (e.g., integer, varchar, timestamp) |
| ↳ `nullable` | boolean | Whether the column allows NULL values |
| ↳ `default` | string | Default value expression |
| ↳ `isPrimaryKey` | boolean | Whether the column is part of the primary key |
| ↳ `isForeignKey` | boolean | Whether the column is a foreign key |
| ↳ `references` | object | Foreign key reference information |
| ↳ `table` | string | Referenced table name |
| ↳ `column` | string | Referenced column name |
| ↳ `primaryKey` | array | Primary key column names |
| ↳ `foreignKeys` | array | Foreign key constraints |
| ↳ `column` | string | Local column name |
| ↳ `referencesTable` | string | Referenced table name |
| ↳ `referencesColumn` | string | Referenced column name |
| ↳ `indexes` | array | Table indexes |
| ↳ `name` | string | Index name |
| ↳ `columns` | array | Columns included in the index |
| ↳ `unique` | boolean | Whether the index enforces uniqueness |
| `schemas` | array | List of available schemas in the database |
