> ## Documentation Index
> Fetch the complete documentation index at: https://docs.keephq.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL

> PostgreSQL Provider is a provider used to query POSTGRES databases

## Authentication

This provider requires authentication.

* **username**: Postgres username (required: True, sensitive: False)
* **password**: Postgres password (required: True, sensitive: True)
* **host**: Postgres hostname (required: True, sensitive: False)
* **database**: Postgres database name (required: False, sensitive: False)
* **port**: Postgres port (required: False, sensitive: False)

Certain scopes may be required to perform specific actions or queries via the provider. Below is a summary of relevant scopes and their use cases:

* **connect\_to\_server**: The user can connect to the server (mandatory)

## In workflows

This provider can be used in workflows.

As "step" to query data, example:

```yaml theme={null}
steps:
    - name: Query postgres
      provider: postgres
      config: "{{ provider.my_provider_name }}"
      with:
        query: {value}  
```

As "action" to make changes or update data, example:

```yaml theme={null}
actions:
    - name: Query postgres
      provider: postgres
      config: "{{ provider.my_provider_name }}"
      with:
        query: {value}  
```

Check the following workflow example:

* [disk\_grown\_defects\_rule.yml](https://github.com/keephq/keep/blob/main/examples/workflows/disk_grown_defects_rule.yml)

## Provider Methods

The provider exposes the following [Provider Methods](/providers/provider-methods#via-ai-assistant). They are available in the [AI Assistant](/overview/ai-incident-assistant).

* **execute\_query** Query the Postgres database (view, scopes: no additional scopes)

## Connecting with the Provider

In order to connect to the Postgres database, you will need to create a new user with the required permissions. Here's how you can do this:

1. Connect to the Postgresql server as a user with sufficient privileges to create a new user.
2. Run the following command to create a new user:
   `CREATE USER '<username>' WITH ENCRYPTED PASSWORD '<password>'`;
3. Run the following command to create a database:
   `CREATE DATABASE '<yourdbname>';`;
4. Grant the necessary permissions to the new user by running the following command:
   `GRANT ALL PRIVILEGES ON <database>.* TO '<username>'`;

## Notes

## Useful Links

* [Postgresql Documentation](https://www.postgresql.org/docs/)
* [Creating user,database and adding access on psql](https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e)
