Dynamic Database Credentials
The database secrets engine is the best argument for Vault. Instead of one shared password for linkly's database, every linkly instance asks Vault for credentials and gets its own PostgreSQL user, valid for an hour, created on the spot and dropped when the lease ends. A leaked password expires by itself, and the database's own logs show exactly which instance used it.
Note: the engine needs a reachable database, which the offline sandbox doesn't have, so the Vault commands are a non-run sample session with realistic output. The SQL that Vault runs is real and runs below in PostgreSQL.
Configure the connection
Vault needs an admin-ish account that may create roles. Give it a dedicated user, then let Vault rotate that user's password so no human knows it anymore:
vault secrets enable database
vault write database/config/linkly-db \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://{{username}}:{{password}}@postgres.linkly.internal:5432/linkly?sslmode=verify-full" \
username="vault_admin" \
password="initial-bootstrap-password" \
allowed_roles="linkly-app,linkly-readonly" \
password_authentication="scram-sha-256"
vault write -force database/rotate-root/linkly-dbSuccess! Data written to: database/config/linkly-db
Success! Data written to: database/rotate-root/linkly-dbAfter rotate-root, the bootstrap password no longer works; only Vault can log in as vault_admin.
A role: what to create, for how long
vault write database/roles/linkly-app \
db_name=linkly-db \
creation_statements=@create-app-user.sql \
revocation_statements=@drop-user.sql \
default_ttl=1h \
max_ttl=24h-- create-app-user.sql: Vault fills in {{name}}, {{password}} and {{expiration}}
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}' IN ROLE linkly_app;-- drop-user.sql: end the user's sessions, then remove it
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = '{{name}}';
DROP ROLE IF EXISTS "{{name}}";Grant table privileges to a group role (linkly_app) once, and make each generated user a member with IN ROLE. Objects then belong to the group, not to short-lived users, and dropping a user never fails because it owns tables.
Asking for credentials
vault read database/creds/linkly-appKey Value
--- -----
lease_id database/creds/linkly-app/QpK2tY7cH1xNw4Lm0aBvZr9E
lease_duration 1h
lease_renewable true
password A1a-mZ7pQ2wX9vK4rT6y
username v-token-linkly-a-Rg3kP8sLw2Yq0TnB4vXe-1793091600Each call creates a new user. The name encodes the auth display name, the role, a random part and the creation time, so a database administrator looking at pg_stat_activity knows where a connection came from.
What happens in PostgreSQL
Here is what Vault's statements do, run against a real PostgreSQL 18. The pg_roles query is what a DBA sees while two linkly instances are running:
SET TIME ZONE 'UTC';
CREATE ROLE linkly_app NOLOGIN;
GRANT SELECT, INSERT ON ALL TABLES IN SCHEMA public TO linkly_app;
-- two linkly pods asked Vault for credentials
CREATE ROLE "v-k8s-linkly-a-Rg3kP8sL-1793091600" WITH LOGIN PASSWORD 'A1a-mZ7pQ2wX9vK4rT6y' VALID UNTIL '2026-10-27 10:00:00+00' IN ROLE linkly_app;
CREATE ROLE "v-k8s-linkly-a-Xw81bQzt-1793091612" WITH LOGIN PASSWORD 'Pq0-zR4nT8vB2mL6wK1c' VALID UNTIL '2026-10-27 10:00:12+00' IN ROLE linkly_app;
SELECT rolname, rolcanlogin, rolvaliduntil
FROM pg_roles WHERE rolname LIKE 'v-%' ORDER BY rolname;
-- the first lease expires: Vault runs the revocation statement
DROP ROLE IF EXISTS "v-k8s-linkly-a-Rg3kP8sL-1793091600";
SELECT count(*) AS vault_users FROM pg_roles WHERE rolname LIKE 'v-%';SET
CREATE ROLE
GRANT
CREATE ROLE
CREATE ROLE
rolname | rolcanlogin | rolvaliduntil
------------------------------------+-------------+------------------------
v-k8s-linkly-a-Rg3kP8sL-1793091600 | t | 2026-10-27 10:00:00+00
v-k8s-linkly-a-Xw81bQzt-1793091612 | t | 2026-10-27 10:00:12+00
(2 rows)
DROP ROLE
vault_users
-------------
1
(1 row)
VALID UNTIL is a second safety net: even if Vault were down when the lease expired, PostgreSQL itself would refuse the password afterwards.
Static roles: when you can't create users
Some applications or vendors require a fixed username. Static roles keep the user but let Vault rotate its password on a schedule:
vault write database/static-roles/reporting \
db_name=linkly-db username=reporting rotation_period=24h
vault read database/static-creds/reportingKey Value
--- -----
last_vault_rotation 2026-10-28T09:00:00.000000Z
password Vk8-wQ3zN1rT5yB7mL2p
rotation_period 24h
ttl 23h59m12s
username reportingStatic roles still beat a password in a wiki: the value changes daily, and only Vault and its authorised readers know it.
Using it from linkly
The application must cope with credentials that change: read them from a file that Vault Agent (or the Agent Injector, or VSO's VaultDynamicSecret) keeps current, and reconnect when the file changes or the pool reports authentication errors. Database connection pools should cap connection lifetime below the lease TTL so old connections don't outlive the user.
Test yourself
Why is a per-instance database user better than a shared password?
- It is faster
- Each credential expires on its own and can be traced and revoked individually
- It needs no database
- It avoids TLS
What does vault write -force database/rotate-root/linkly-db do?
- Rotates every application's password
- Changes the password of the account Vault uses to connect, so only Vault knows it
- Deletes the database
- Renews all leases
Why grant privileges to a group role and add generated users with IN ROLE?
- PostgreSQL requires it
- Objects and grants stay with the group, so short-lived users can be dropped cleanly
- It makes passwords longer
- It disables VALID UNTIL