Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

7.6 KB
Raw Blame
---
title: "PostgreSQL dynamic secrets"
sidebarTitle: "PostgreSQL"
description: "Learn how to dynamically generate PostgreSQL and CockroachDB database users."
---

import DynamicSecretUsernameTemplateParamField from "/snippets/documentation/platform/dynamic-secrets/dynamic-secret-username-template-field.mdx";

The Infisical PostgreSQL dynamic secret allows you to generate PostgreSQL database credentials on demand based on configured role.

You can also use this dynamic secret with CockroachDB, as described in [Use with CockroachDB](#use-with-cockroachdb).

## Prerequisite

Create a user with the required permission in your SQL instance. This user will be used to create new accounts on-demand.


## Set up dynamic secrets with PostgreSQL

<Steps>
  <Step title="Open Secret Overview Dashboard">
	Open the Secret Overview dashboard and select the environment in which you would like to add a dynamic secret.
  </Step>
  <Step title="Click on the 'Add Dynamic Secret' button">
	![Add Dynamic Secret Button](../../../images/platform/dynamic-secrets/add-dynamic-secret-button.png)
  </Step>
  <Step title="Select `SQL Database`">
	![Dynamic Secret Modal](../../../images/platform/dynamic-secrets/dynamic-secret-modal.png)
  </Step>
  <Step title="Provide the inputs for dynamic secret parameters">
	<ParamField path="Secret Name" type="string" required>
		Name by which you want the secret to be referenced
	</ParamField>

	<ParamField path="Default TTL" type="string" required>
		Default time-to-live for a generated secret (it is possible to modify this value after a secret is generated)
	</ParamField>

	<ParamField path="Max TTL" type="string" required>
		Maximum time-to-live for a generated secret
	</ParamField>

	<ParamField path="Metadata" type="list" required>
		List of key/value metadata pairs
	</ParamField>

	<ParamField path="Service" type="string" required>
		Choose the service you want to generate dynamic secrets for. This must be selected as **PostgreSQL**.
	</ParamField>

	<ParamField path="Host" type="string" required>
		Database host
	</ParamField>

	<ParamField path="Port" type="number" required>
		Database port
	</ParamField>

	<ParamField path="User" type="string" required>
		Username that will be used to create dynamic secrets
	</ParamField>

	<ParamField path="Password" type="string" required>
		Password that will be used to create dynamic secrets
	</ParamField>

	<ParamField path="Database Name" type="string" required>
		Name of the database for which you want to create dynamic secrets
	</ParamField>

	<ParamField path="CA (SSL)" type="string">
		A CA may be required if your DB requires it for incoming connections. AWS RDS instances with default settings will requires a CA which can be downloaded [here](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html#UsingWithRDS.SSL.CertificatesAllRegions).
	</ParamField>

	<ParamField path="SSL Reject Unauthorized" type="boolean" default="true">
		If enabled, the server certificate will be verified against the list of supplied CAs. Disable this option if you are using a self-signed certificate.
	</ParamField>

	![Dynamic Secret Setup Modal](../../../images/platform/dynamic-secrets/dynamic-secret-setup-modal-postgresql.png)

  </Step>
  <Step title="(Optional) Modify SQL Statements">
	![Modify SQL Statements Modal](../../../images/platform/dynamic-secrets/modify-sql-statements.png)
	<DynamicSecretUsernameTemplateParamField />
	<ParamField path="Customize SQL Statement" type="string">
  	If you want to provide specific privileges for the generated dynamic credentials, you can modify the SQL statement to your needs. This is useful if you want to only give access to a specific table(s).
	</ParamField>
  </Step>
  <Step title="Click 'Submit'">
  	After submitting the form, you'll see a dynamic secret created in the dashboard.

	<Note>
		If this step fails, you may have to add the CA certificate.
	</Note>

	![Dynamic Secret](../../../images/platform/dynamic-secrets/dynamic-secret.png)
  </Step>
  <Step title="Generate dynamic secrets">
	Once you've successfully configured the dynamic secret, you're ready to generate on-demand credentials.
	To do this, simply select the 'Generate' button which appears when hovering over the dynamic secret item.
	Alternatively, you can initiate the creation of a new lease by selecting 'New Lease' from the dynamic secret lease list section.

	![Dynamic Secret](/images/platform/dynamic-secrets/dynamic-secret-generate.png)
	![Dynamic Secret](/images/platform/dynamic-secrets/dynamic-secret-lease-empty.png)

	When generating these secrets, it's important to specify a Time-to-Live (TTL) duration. This will dictate how long the credentials are valid for.

	![Provision Lease](/images/platform/dynamic-secrets/provision-lease.png)

	<Tip>
		Ensure that the TTL for the lease falls within the maximum TTL defined when configuring the dynamic secret.
	</Tip>


	Once you click the `Submit` button, a new secret lease will be generated and the credentials for it will be shown to you.

	![Provision Lease](/images/platform/dynamic-secrets/lease-values.png)
  </Step>
</Steps>

## Use with CockroachDB

CockroachDB accepts PostgreSQL connections, so you can follow the [PostgreSQL setup steps](#set-up-dynamic-secrets-with-postgresql) with these settings:

- **Service**: select **PostgreSQL**
- **Port**: your cluster's SQL port (`26257` by default)
- **CA (SSL)**: your cluster's CA certificate, such as `ca.crt` from the cluster's certificates directory (required, because CockroachDB refuses unencrypted connections)

### Admin user privileges

Infisical uses the user you enter in **User** to create a database user for each lease and grant it access to your tables. To give that user the privileges it needs, connect to the database in **Database Name** as a CockroachDB admin and run:

```sql
ALTER USER <admin-user> WITH CREATEROLE CREATELOGIN;
GRANT ALL ON ALL TABLES IN SCHEMA public TO <admin-user> WITH GRANT OPTION;
ALTER DEFAULT PRIVILEGES FOR ROLE <table-owner> IN SCHEMA public GRANT ALL ON TABLES TO <admin-user> WITH GRANT OPTION;
```

Replace `<table-owner>` with the user that creates tables in this database, such as the user your migrations run as. The `GRANT` statement only covers tables that already exist, so without the last line, every lease creation fails after a new table is added.

If your creation statement grants fewer privileges, such as only `SELECT`, replace `ALL` with those privileges in the last two statements.

### SQL statements

Add `SET autocommit_before_ddl = false;` as the first line of **Creation Statement**:

```sql
SET autocommit_before_ddl = false;
CREATE USER "{{username}}" WITH ENCRYPTED PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{username}}";
```

Without that line, CockroachDB can commit `CREATE USER` before it runs `GRANT`, so if `GRANT` fails, the new user stays in your cluster with no lease you can revoke.

**Renew Statement** and **Revocation Statement** work on CockroachDB without changes.

## Audit or revoke leases
Once you have created one or more leases, you'll be able to access them by selecting the respective dynamic secret item on the dashboard.
This will allow you to see the expiration time of the lease or delete the lease before it's set time to live.

![Provision Lease](/images/platform/dynamic-secrets/lease-data.png)

## Renew leases
To extend the life of the generated dynamic secret leases past its initial time to live, simply select the **Renew** button as illustrated below.
![Provision Lease](/images/platform/dynamic-secrets/dynamic-secret-lease-renew.png)

<Warning>
	Lease renewals can't exceed the maximum TTL set when configuring the dynamic secret
</Warning>