> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/integrations/catalog/postgresql.md).

# PostgreSQL

Query, insert, update, delete data, and manage schema in a PostgreSQL database.

### Connectors

Choose the connector that matches your authentication method. The options below use the names shown in StackAI.

| Connector  | Authentication |
| ---------- | -------------- |
| PostgreSQL | Credentials    |

#### PostgreSQL

Connect to PostgreSQL databases to query and manage your relational data. Supports advanced SQL features, JSON storage, and complex data types.

**Set up in PostgreSQL**

1. Prepare the PostgreSQL account or service that the workflow will access. Grant access to the required resources and operations.
2. Obtain the account, endpoint, and authentication values listed below from the service administrator.

**Set up in StackAI**

1. Open **Settings → Connections**, add a connection, and select **PostgreSQL**.
2. Enter the credentials and connection details in the fields below.
3. Save the connection and check its connection status. Select this saved connection when configuring an action or feature that uses it.

**Connection fields**

| StackAI field | Required | What to enter                                                                                                                                                                                                                                                                                                                                                                                                   |
| ------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Host          | Yes      | The hostname or IP address of your PostgreSQL server. [Provider instructions](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING).                                                                                                                                                                                                                                                     |
| Port          | Yes      | The port number on which PostgreSQL is running.                                                                                                                                                                                                                                                                                                                                                                 |
| Username      | Yes      | PostgreSQL user for this integration. Use a read-only role with SELECT-only privileges on the schemas you query. Example: CREATE ROLE stackai\_ro LOGIN PASSWORD '…'; GRANT CONNECT ON DATABASE mydb TO stackai\_ro; GRANT USAGE ON SCHEMA public TO stackai\_ro; GRANT SELECT ON ALL TABLES IN SCHEMA public TO stackai\_ro; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO stackai\_ro;. |
| Password      | Yes      | Password for the read-only PostgreSQL user above. Credentials should belong to a SELECT-only role.                                                                                                                                                                                                                                                                                                              |
| Database      | Yes      | The name of the PostgreSQL database to connect to.                                                                                                                                                                                                                                                                                                                                                              |

**Provider documentation:** [Setup reference](https://www.postgresql.org/docs/current/libpq-connect.html).

#### Verify the connection

Check that the connection is healthy. Then run a read operation against a resource the connected account can access. A successful connection check confirms authentication; it does not guarantee permission for every action.

If access is denied, check the credential or OAuth permissions, the selected account or environment, and access to the specific resource. Update expired credentials or reconnect after permissions change.

### Use an action

1. Add the required **PostgreSQL** action to the workflow.
2. Select a connection when the action requests one.
3. Complete the required fields. Use the reference picker when a value comes from another workflow step.
4. Run the workflow and inspect the action’s output. Check the result before using it in a later step.

### Actions

#### Query PostgreSQL

Query a PostgreSQL database with a plain english query or a SQL query

**Inputs**

| Field  | Type                      | Required | Description                                                                                 |
| ------ | ------------------------- | -------- | ------------------------------------------------------------------------------------------- |
| Schema | multiple\_select (string) | No       | Database Schema (tables, columns, types, etc.). Options load from the selected connection.  |
| Query  | string                    | Yes      | Enter your query in plain English or SQL format to execute against the database. Default: . |

**Outputs**

| Field     | Type        | Description                                          |
| --------- | ----------- | ---------------------------------------------------- |
| Query     | string      | The SQL query that was executed.                     |
| Results   | dict\_array | The results of the query.                            |
| Truncated | boolean     | True if the result set was truncated by the row cap. |

#### Modify PostgreSQL Data

Insert, update, or delete rows in a PostgreSQL database table. General mode provides a guided form; Advanced mode accepts raw DML SQL.

**Inputs**

| Field      | Type            | Required | Description                                                                                                                                               |
| ---------- | --------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Mode       | select (string) | Yes      | General mode provides a guided form; Advanced mode accepts raw DML SQL. Available values: General, Advanced. Default: general.                            |
| SQL Query  | string          | No       | Raw DML SQL statement to execute. Only DML statements are allowed (INSERT, UPDATE, DELETE). WITH (CTE) expressions are supported.                         |
| Operation  | select (string) | No       | The DML operation to perform. Available values: Insert, Update, Delete.                                                                                   |
| Table Name | select (string) | No       | The target table. Options load from the selected connection.                                                                                              |
| Data       | string          | No       | JSON data for the operation (keys are column names). Required for Insert and Update. Example: {"column1": "value1"}.                                      |
| Condition  | string          | No       | JSON object for the WHERE clause (keys are column names). All conditions are combined with AND. Example: {"name": "John", "email": "<john@example.com>"}. |

**Outputs**

| Field         | Type    | Description                                    |
| ------------- | ------- | ---------------------------------------------- |
| SQL Query     | string  | The parameterized SQL query that was executed. |
| Rows Affected | integer | Number of rows affected by the operation.      |
| Success       | boolean | Whether the operation was successful.          |

#### Manage PostgreSQL Schema

Manage PostgreSQL schema objects — tables, indexes, views, sequences, and more. General mode provides a guided form; Advanced mode accepts raw DDL SQL.

**Inputs**

| Field           | Type            | Required | Description                                                                                                                    |
| --------------- | --------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------ |
| Mode            | select (string) | Yes      | General mode provides a guided form; Advanced mode accepts raw DDL SQL. Available values: General, Advanced. Default: general. |
| SQL Query       | string          | No       | Raw DDL SQL statement to execute. Only DDL statements are allowed (CREATE, ALTER, DROP, TRUNCATE, COMMENT, RENAME).            |
| Operation       | select (string) | No       | The DDL operation to perform. Available values: Create Table, Alter Table, Drop Table.                                         |
| Table Name      | string          | No       | The target table name.                                                                                                         |
| Columns         | string          | No       | JSON object mapping column names to their types. Example: {"id": "SERIAL PRIMARY KEY", "name": "TEXT NOT NULL"}.               |
| Alter Type      | select (string) | No       | The type of alteration to perform. Available values: Add Column, Drop Column, Rename Column.                                   |
| Column Name     | string          | No       | The column to add, drop, or rename.                                                                                            |
| Column Type     | string          | No       | The SQL type for the new column (used with Add Column).                                                                        |
| New Column Name | string          | No       | The new name for the column (used with Rename Column).                                                                         |

**Outputs**

| Field     | Type    | Description                              |
| --------- | ------- | ---------------------------------------- |
| SQL Query | string  | The DDL SQL statement that was executed. |
| Success   | boolean | Whether the operation was successful.    |

### Important behavior

Actions that create, update, send, or delete data can change the connected service. Review their inputs before running them. A retry can repeat an operation; consider this when configuring retries for actions that change data.

The catalog lists the fields and capabilities available in the supplied provider definition. Availability can also depend on your deployment and organization’s Feature Access settings.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/integrations/catalog/postgresql.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
