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

# PostgreSQL credentials rotation

> Learn how to automatically rotate PostgreSQL credentials.

## Prerequisites

1. Create a [PostgreSQL Connection](https://infisical.com/docs/integrations/app-connections/postgres.md) with the required **Secret Rotation** permissions

2. Create two designated database users for Infisical to rotate the credentials for. Be sure to grant each user login permissions for the desired database with the necessary privileges their use case will require.

   > [!NOTE]
   >
   > Rotation alternates between these two users, changing one password per cycle so that the other stays valid while your applications pick up the new credentials. See [why two users are required](https://infisical.com/docs/documentation/platform/secret-rotation/overview#why-two-users).

   An example creation statement might look like:

   ```SQL theme={"dark"}
   -- create user roles
   CREATE USER infisical_user_1 WITH ENCRYPTED PASSWORD 'temporary_password';
   CREATE USER infisical_user_2 WITH ENCRYPTED PASSWORD 'temporary_password';

   -- grant database connection permissions
   GRANT CONNECT ON DATABASE my_database TO infisical_user_1;
   GRANT CONNECT ON DATABASE my_database TO infisical_user_2;

   -- grant relevant table permissions
   GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO infisical_user_1;
   GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO infisical_user_2;
   ```

   > [!TIP]
   >
   > To learn more about PostgreSQL's permission system, please visit their [documentation](https://www.postgresql.org/docs/current/sql-grant.html).

3. Ensure your network security policies allow incoming requests from Infisical to this rotation provider, if network restrictions apply.

## Create a PostgreSQL credentials rotation in Infisical

> [tab] Infisical UI
>
> 1. Navigate to your Secrets Management Project's Dashboard and select **Add Secret Rotation** from the actions dropdown.
>
>    ![Secrets Management Dashboard](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/generic/add-secret-rotation.png)
>
> 2. Select the **PostgreSQL Credentials** option.
>
>    ![Select PostgreSQL Credentials](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/select-postgres-credentials-option.png)
>
> 3. Select the **PostgreSQL Connection** to use and configure the rotation behavior. Then click **Next**.
>
>    ![Rotation Configuration](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-configuration.png)
>
>    * **PostgreSQL Connection** - the connection that will perform the rotation of the configured database user credentials.
>    * **Rotation Interval** - the interval, in days, that once elapsed will trigger a rotation.
>    * **Rotate At** - the local time of day when rotation should occur once the interval has elapsed.
>    * **Auto-Rotation Enabled** - whether secrets should automatically be rotated once the rotation interval has elapsed. Disable this option to manually rotate secrets or pause secret rotation.
>
> 4. Input the usernames of the database users created above that will be used for rotation. Then click **Next**.
>
>    ![Rotation Parameters](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-parameters.png)
>
>    * **Database Username 1** - the username of the first user that will be used for rotation.
>
>    * **Database Username 2** - the username of the second user that will be used for rotation.
>
> > [!WARNING]
> >
> > If your database is hosted on **PlanetScale**, you must include the branch ID in the username using the format `username.branch_id`.
> >
> > To find your branch ID, use the [PlanetScale CLI](https://planetscale.com/docs/reference/planetscale-cli):
> >
> > ```bash theme={"dark"}
> > pscale org switch <org-name>
> > pscale branch show <database> <branch-name> --format json
> > ```
>
> ![Rotation Advance Parameters](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-advance-parameters.png)
>
> * **Rotation Statement** - the template string query to generate a password for the rotated user.
>
> * **Password Requirements** - the requirements for the passwords Infisical generates for the two PostgreSQL users.
>
> 5. Specify the secret names that the active credentials should be mapped to. Then click **Next**.
>
>    ![Rotation Secrets Mapping](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-secrets-mapping.png)
>
>    * **Username** - the name of the secret that the active username will be mapped to.
>    * **Password** - the name of the secret that the active password will be mapped to.
>
> 6. Give your rotation a name and description (optional). Then click **Next**.
>
>    ![Rotation Details](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-details.png)
>
>    * **Name** - the name of the secret rotation configuration. Must be slug-friendly.
>    * **Description** (optional) - a description of this rotation configuration.
>
> 7. Review your configuration, then click **Create Secret Rotation**.
>
>    ![Rotation Review](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-confirm.png)
>
> 8. Your **PostgreSQL Credentials** are now available for use via the mapped secrets.
>
>    ![Rotation Created](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/postgres-credentials/postgres-credentials-created.png)

> [tab] API
>
> To create a PostgreSQL Credentials Rotation, make an API request to the [Create PostgreSQL
> Credentials Rotation](https://infisical.com/docs/api-reference/endpoints/secret-rotations/postgres-credentials/create.md) API endpoint.
>
> ### Sample request
>
> ```bash Request theme={"dark"}
> curl --request POST \
> --url https://us.infisical.com/api/v2/secret-rotations/postgres-credentials \
> --header 'Content-Type: application/json' \
> --data '{
>     "name": "my-pg-rotation",
>     "projectId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>     "description": "my database credentials rotation",
>     "connectionId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>     "environment": "dev",
>     "secretPath": "/",
>     "isAutoRotationEnabled": true,
>     "rotationInterval": 30,
>     "rotateAtUtc": {
>         "hours": 0,
>         "minutes": 0
>     },
>     "parameters": {
>         "username1": "infisical_user_1",
>         "username2": "infisical_user_2"
>     },
>     "secretsMapping": {
>         "username": "POSTGRES_DB_USERNAME",
>         "password": "POSTGRES_DB_PASSWORD"
>     }
> }'
> ```
>
> ### Sample response
>
> ```bash Response theme={"dark"}
> {
>     "secretRotation": {
>         "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>         "name": "my-pg-rotation",
>         "description": "my database credentials rotation",
>         "secretsMapping": {
>             "username": "POSTGRES_DB_USERNAME",
>             "password": "POSTGRES_DB_PASSWORD"
>         },
>         "isAutoRotationEnabled": true,
>         "activeIndex": 0,
>         "folderId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>         "connectionId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>         "createdAt": "2023-11-07T05:31:56Z",
>         "updatedAt": "2023-11-07T05:31:56Z",
>         "rotationInterval": 30,
>         "rotationStatus": "success",
>         "lastRotationAttemptedAt": "2023-11-07T05:31:56Z",
>         "lastRotatedAt": "2023-11-07T05:31:56Z",
>         "lastRotationJobId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>         "nextRotationAt": "2023-11-07T05:31:56Z",
>         "connection": {
>             "app": "postgres",
>             "name": "my-pg-connection",
>             "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
>         },
>         "environment": {
>             "slug": "dev",
>             "name": "Development",
>             "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
>         },
>         "projectId": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>         "folder": {
>             "id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
>             "path": "/"
>         },
>         "rotateAtUtc": {
>             "hours": 0,
>             "minutes": 0
>         },
>         "lastRotationMessage": null,
>         "type": "postgres-credentials",
>         "parameters": {
>             "username1": "infisical_user_1",
>             "username2": "infisical_user_2"
>         }
>     }
> }
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.