# PostgreSQL (/integrations/postgresql)

Use cases and templates: [PostgreSQL integration for AI agents](https://www.sim.ai/integrations/postgresql)





<BlockInfoCard type="postgresql" color="#336791" />

{/* MANUAL-CONTENT-START:intro */}

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.

{/* MANUAL-CONTENT-END */}

## 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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
| - | - | - | - |
| `host` | string | Yes | PostgreSQL server hostname or IP address |
| `port` | number | Yes | PostgreSQL server port (default: 5432) |
| `database` | string | Yes | Database name to connect to |
| `username` | string | Yes | Database username |
| `password` | string | Yes | Database password |
| `ssl` | string | No | SSL connection mode (disabled, required, preferred) |
| `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 |
