Database Secrets Engine
You'll learn how to configure Vault's Database Secrets Engine to generate short-lived credentials for PostgreSQL, MySQL, and other supported databases on-demand.
What Is the Database Secrets Engine?
The Database Secrets Engine is one of Vault's most powerful dynamic secrets features. Instead of sharing static database passwords across your applications, this engine connects to your database and creates temporary credentials with specific permissions whenever an application requests access. When the lease expires, Vault automatically revokes those credentials.
Think of it like a hotel key card system: instead of giving everyone a master key that works forever, the front desk issues temporary cards that expire after checkout. If a card is lost, it stops working automatically—no need to change all the locks.
Configuration Steps
First, enable the database secrets engine at a specific path:
vault secrets enable database
Next, configure a database connection. For PostgreSQL:
vault write database/config/my-postgresql-database \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://{{username}}:{{password}}@postgres.example.com:5432/mydb" \
allowed_roles="readonly,readwrite" \
username="vault-admin" \
password="admin-password"
For MySQL, the process is similar with the mysql-database-plugin.
Then create a role that defines what permissions dynamically generated users receive:
vault write database/roles/readonly \
db_name=my-postgresql-database \
creation_statements="CREATE USER '{{name}}' WITH PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; \
GRANT SELECT ON ALL TABLES IN SCHEMA public TO '{{name}}';" \
default_ttl="1h" \
max_ttl="24h"
Applications read credentials from database/creds/readonly, receiving unique, time-limited username/password pairs.
Key Takeaway: The Database Secrets Engine eliminates static credentials by generating temporary database users on-demand with automatic cleanup, dramatically reducing credential exposure risk.