1--- 2layout: "docs" 3page_title: "Database - Secrets Engines" 4sidebar_title: "Databases" 5sidebar_current: "docs-secrets-databases" 6description: |- 7 The database secrets engine generates database credentials dynamically based 8 on configured roles. It works with a number of different databases through a 9 plugin interface. There are a number of builtin database types and an exposed 10 framework for running custom database types for extendability. 11--- 12 13# Databases 14 15The database secrets engine generates database credentials dynamically based on 16configured roles. It works with a number of different databases through a plugin 17interface. There are a number of builtin database types and an exposed framework 18for running custom database types for extendability. This means that services 19that need to access a database no longer need to hardcode credentials: they can 20request them from Vault, and use Vault's leasing mechanism to more easily roll 21keys. 22 23Since every service is accessing the database with unique credentials, it makes 24auditing much easier when questionable data access is discovered. You can track 25it down to the specific instance of a service based on the SQL username. 26 27Vault makes use of its own internal revocation system to ensure that users 28become invalid within a reasonable time of the lease expiring. 29 30### Static Roles 31 32The database secrets engine supports the concept of "static roles", which are 33a 1-to-1 mapping of Vault Roles to usernames in a database. The current password 34for the database user is stored and automatically rotated by Vault on a 35configurable period of time. This is in contrast to dynamic secrets, where a 36unique username and password pair are generated with each credential request. 37When credentials are requested for the Role, Vault returns the current 38password for the configured database user, allowing anyone with the proper 39Vault policies to have access to the user account in the database. 40 41Not all database types support static roles at this time. Please consult the 42specific database documentation on the left navigation to see if a given 43database backend supports static roles. 44 45 46## Setup 47 48Most secrets engines must be configured in advance before they can perform their 49functions. These steps are usually completed by an operator or configuration 50management tool. 51 521. Enable the database secrets engine: 53 54 ```text 55 $ vault secrets enable database 56 Success! Enabled the database secrets engine at: database/ 57 ``` 58 59 By default, the secrets engine will enable at the name of the engine. To 60 enable the secrets engine at a different path, use the `-path` argument. 61 621. Configure Vault with the proper plugin and connection information: 63 64 ```text 65 $ vault write database/config/my-database \ 66 plugin_name="..." \ 67 connection_url="..." \ 68 allowed_roles="..." \ 69 username="..." \ 70 password="..." 71 ``` 72 73 This secrets engine can configure multiple database connections. For details 74 on the specific configuration options, please see the database-specific 75 documentation. 76 771. Configure a role that maps a name in Vault to an SQL statement to execute to create the database credential: 78 79 ```text 80 $ vault write database/roles/my-role \ 81 db_name=my-database \ 82 creation_statements="..." \ 83 default_ttl="1h" \ 84 max_ttl="24h" 85 Success! Data written to: database/roles/my-role 86 ``` 87 88 The `{{name}}` and `{{password}}` fields will be populated by the plugin 89 with dynamically generated values. In some plugins the `{{expiration}}` 90 field is also be supported. 91 92## Usage 93 94After the secrets engine is configured and a user/machine has a Vault token with 95the proper permission, it can generate credentials. 96 971. Generate a new credential by reading from the `/creds` endpoint with the name 98of the role: 99 100 ```text 101 $ vault read database/creds/my-role 102 Key Value 103 --- ----- 104 lease_id database/creds/my-role/2f6a614c-4aa2-7b19-24b9-ad944a8d4de6 105 lease_duration 1h 106 lease_renewable true 107 password 8cab931c-d62e-a73d-60d3-5ee85139cd66 108 username v-root-e2978cd0- 109 ``` 110 111## Custom Plugins 112 113This secrets engine allows custom database types to be run through the exposed 114plugin interface. Please see the [custom database 115plugin](/docs/secrets/databases/custom.html) for more information. 116 117## API 118 119The database secrets engine has a full HTTP API. Please see the [Database secret 120secrets engine API](/api/secret/databases/index.html) for more details. 121