SecretsVault · PostgreSQL
Dynamic database credentials with HashiCorp Vault
Vault's database secrets engine generates database credentials on request, from roles you configure. Each credential comes with a lease, and Vault revokes it when the lease expires.

How it works
The database secrets engine creates database credentials on demand, from the roles you configure. Services no longer hardcode credentials. They request them from Vault and use Vault's leasing mechanism.
Every service accesses the database with unique credentials. HashiCorp's documentation points out what that does for auditing: the SQL username leads from a questionable data access to the exact service instance behind it.
1. Enable the engine and connect it
HashiCorp highly recommends creating a user within the database specifically for Vault. Vault uses it to manipulate the dynamic users. The documentation calls it the "root" user.
vault secrets enable database
vault write database/config/my-postgresql-database \
plugin_name="postgresql-database-plugin" \
allowed_roles="my-role" \
connection_url="postgresql://{{username}}:{{password}}@localhost:5432/database-name" \
username="vaultuser" \
password="<password>" \
password_authentication="scram-sha-256"This is the documented command. The connection name, the host, the port, the database name and the user are yours to replace. Keep {{username}} and {{password}} in the URL as written: root credential rotation requires a connection URL that is templated.
allowed_roleslists the roles allowed to use this connection. It defaults to empty, which means no roles. A*lets any role use it.password_authentication="scram-sha-256"makes Vault hash passwords before PostgreSQL stores them. It requires PostgreSQL 10 as a minimum. The default ispassword, which sends passwords to PostgreSQL in plaintext, where they may appear in PostgreSQL logs.
2. Create a role
vault write database/roles/my-role \
db_name="my-postgresql-database" \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' \
VALID UNTIL '{{expiration}}'; \
GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
default_ttl="1h" \
max_ttl="24h"A role maps a name in Vault to the SQL statements that create the database credential. Vault substitutes the {{name}}, {{password}} and {{expiration}} values. Change the GRANT to what your application needs.
default_ttlspecifies the TTL for the leases associated with the role.max_ttlspecifies the maximum TTL for those leases. The values1hand24hare the ones in HashiCorp's example, so choose your own.revocation_statementsis optional. If it is not provided, the plugin defaults to a generic drop user statement.
3. Read credentials
A user or a machine holding a Vault token with the right permission gets a credential by reading the creds endpoint for the role's name:
vault read database/creds/my-roleKey Value
--- -----
lease_id database/creds/my-role/<lease-uuid>
lease_duration 1h
lease_renewable true
password <generated-password>
username <generated-username>Leases and revocation
With every dynamic secret, Vault creates a lease: metadata that includes a time duration and renewability. Lease IDs are structured so that their prefix is always the path the secret was requested from. That lets you revoke trees of secrets.
vault lease renew -increment=3600 <lease-id>
vault lease revoke <lease-id>
vault lease revoke -prefix database/creds/my-role- Renewal. The increment counts from the current time and does not extend the end of the current TTL. The requested increment is completely advisory.
- Revocation. Revoking a lease invalidates the secret immediately and prevents any further renewals. HashiCorp calls prefix revocation very useful after an intrusion into one specific system.
- Expiry. When a lease is expired, Vault automatically revokes it. Whoever consumes a secret has to return to Vault regularly, either to renew the lease or to ask for a replacement secret.
If Vault cannot revoke a lease, the lease could eventually become an irrevocable lease. vault lease revoke -force -prefix force revokes such a lease.
Root credential rotation
vault write -force database/rotate-root/my-postgresql-databaseHashiCorp strongly recommends this step once configuration is done. After it, Vault itself is the only party able to use the Vault user. When it is done, the password for that user is no longer accessible. Because of this, the documentation highly recommends a user created specifically for Vault. When it rotates passwords, Vault treats standard credentials and root credentials the same way.
What to check
- The connection uses a database user created specifically for Vault, and its password has been rotated with
rotate-root. password_authenticationis set toscram-sha-256.allowed_rolesnames the roles that need the connection and does not use*.- Each role sets its own
default_ttlandmax_ttl. - Each client renews its lease or requests a replacement before the lease expires.
- No lease has become irrevocable.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

HashiCorp Vault: what to set up first
Secrets · 4 min read
Storage, seal, audit, auth, policies and KV version 2, in the order a new Vault server needs them.

Kubernetes Secrets: encryption at rest and who can read them
Security · 4 min read
How Secrets are stored, how to encrypt them in etcd, and who can still read them.

AWS Secrets Manager or Parameter Store
Secrets · 4 min read
Rotation, cross-account access, size limits and cost decide which AWS service holds a given value.