# PostgresConnector

## Configuration

Prism uses the `psycopg2-binary` module to instantiate and configure the Postgres connector. The required arguments rare:

* `id`: ID for your connector.
* `autocommit`: toggle whether the connection commits changes to the database after executing. The default is `True`.
* `host`: the hostname of Postgres database.
* `port`: the port number of the Postgres database.
* `database`: the name of the database to which you want to connect.
* `user`: the user name to use for authentication.
* `password`: the password to use for authentication

```python
postgres_connector = PostgresConnector(
    id="postgres_connector_id",
    user=os.environ["POSTGRES_USER"],
    password=os.environ["POSTGRES_PASSWORD"],
    port=5432,
    host=os.environ["POSTGRES_HOST"],
    database=os.environ["POSTGRES_DB"],
    autocommit=True,
)
```

## `execute_sql`

You can run queries against the Postgres database using the `execute_sql` function:

```python
from prism.decorators import task
from prism.runtime import CurrentRun

@task()
def postgres_task(self):
    conn = CurrentRun.conn("postgres_connector_id")
    data = conn.execute_sql(
        sql="SELECT * FROM table"
    )
```

Note that when `return_type = None`, the result will be a list tuples containing the query data.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.runprism.com/connectors/postgresconnector.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
