Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Dynamic Secrets and Encryption

Vault Database Secrets Engine: Dynamic Database Credentials

Configure the Vault database secrets engine: connection config, roles with creation statements, dynamic and static roles, root credential rotation, and a statement template previewer.

The database secrets engine is the classic dynamic secrets use case. You tell Vault how to connect to a database and how to create a user. After that, every application asks Vault for a username and password that exist only for a short time. Vault supports PostgreSQL, MySQL/MariaDB, Microsoft SQL Server, Oracle, MongoDB, Redis, Snowflake, Cassandra and others through plugins.

No database or Vault runs on this site. Commands and SQL are shown read-only as you would run them; the JavaScript examples are stand-ins that render templates and model rotation.

Step 1: enable the engine and configure a connection

Terminal
vault secrets enable database

vault write database/config/appdb \
    plugin_name=postgresql-database-plugin \
    allowed_roles="readonly,readwrite" \
    connection_url="postgresql://{{username}}:{{password}}@db.internal:5432/appdb" \
    username="vault_admin" \
    password="initial-password"

The vault_admin account needs the right to create and drop roles. Because the password above was typed by a human, rotate it immediately so only Vault knows it:

Terminal
vault write -f database/rotate-root/appdb

Step 2: define roles

A role holds SQL statements with placeholders. Vault fills in {{name}}, {{password}} and {{expiration}}.

Terminal
vault write database/roles/readonly \
    db_name=appdb \
    creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
    revocation_statements="REVOKE ALL ON ALL TABLES IN SCHEMA public FROM \"{{name}}\"; DROP ROLE IF EXISTS \"{{name}}\";" \
    default_ttl="1h" max_ttl="24h"

Notice the principle of least privilege: the readonly role gets only SELECT. Create separate roles for jobs that write.

Step 3: fetch credentials

Terminal
vault read database/creds/readonly

Each call returns a different username such as v-token-readonly-8Hq... with its own lease (see leases). Access is controlled by policy:

HCL
path "database/creds/readonly" {
  capabilities = ["read"]
}
JS Stand-in: render creation statements

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Static roles and rotation

Some systems cannot have users created and dropped (legacy applications with a fixed account name). A static role maps to an existing database user. Vault rotates its password on a schedule and serves the current one:

Terminal
vault write database/static-roles/reporting \
    db_name=appdb username="reporting_app" rotation_period="24h"
vault read database/static-creds/reporting

A static role has no lease, so the consumer must read it again after each rotation.

JS Stand-in: static role rotation

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Operational notes

  • Connection limits. Each dynamic user is a new login; size max_connections for your app count.
  • Revocation can fail if a user owns objects or holds sessions; write statements that terminate sessions or reassign ownership first.
  • TLS between Vault and the database is strongly recommended.
  • Root rotation and plugin versions should be part of your maintenance routine. See backup and monitoring.
  • Limit who can write database/config/* and database/roles/*; they can run SQL as the admin user.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why rotate the root credential right after configuring a connection?

    The initial password was typed by a person; rotation removes that knowledge.
  2. 2. Which placeholders can creation statements use?

    Vault fills name, password and expiration for each generated user.
  3. 3. When is a static role the right choice?

    Static roles rotate the password of an existing user.
  4. 4. What policy allows an app to fetch credentials for the readonly role?

    Generating credentials is a read on database/creds/<role>.

Summary

  • Enable the engine, configure a connection, rotate the root credential.
  • Roles hold creation and revocation SQL with placeholders, plus TTLs.
  • Apps read database/creds/<role>; static roles serve rotated fixed accounts.
  • Plan for connection load and for revocation edge cases.

Next: Transit: Encryption as a Service