2017-03-08 02:47:23 +00:00
|
|
|
|
---
|
2020-01-18 00:18:09 +00:00
|
|
|
|
layout: docs
|
|
|
|
|
page_title: PostgreSQL - Storage Backends - Configuration
|
2017-03-08 02:47:23 +00:00
|
|
|
|
description: |-
|
|
|
|
|
The PostgreSQL storage backend is used to persist Vault's data in a PostgreSQL
|
|
|
|
|
server or cluster.
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# PostgreSQL Storage Backend
|
|
|
|
|
|
|
|
|
|
The PostgreSQL storage backend is used to persist Vault's data in a
|
|
|
|
|
[PostgreSQL][postgresql] server or cluster.
|
|
|
|
|
|
2019-05-10 16:48:42 +00:00
|
|
|
|
- **High Availability** – the PostgreSQL storage backend supports
|
|
|
|
|
high availability. Requires PostgreSQL 9.5 or later.
|
2017-03-08 02:47:23 +00:00
|
|
|
|
|
|
|
|
|
- **Community Supported** – the PostgreSQL storage backend is supported by the
|
|
|
|
|
community. While it has undergone review by HashiCorp employees, they may not
|
|
|
|
|
be as knowledgeable about the technology. If you encounter problems with them,
|
|
|
|
|
you may be referred to the original author.
|
|
|
|
|
|
|
|
|
|
```hcl
|
2017-03-08 14:17:00 +00:00
|
|
|
|
storage "postgresql" {
|
2017-03-08 02:47:23 +00:00
|
|
|
|
connection_url = "postgres://user123:secret123!@localhost:5432/vault"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-01-18 00:18:09 +00:00
|
|
|
|
~> **Note:** The PostgreSQL storage backend plugin will attempt to use SSL
|
|
|
|
|
when connecting to the database. If SSL is not enabled the `connection_url`
|
|
|
|
|
will need to be configured to disable SSL. See the documentation below
|
2019-07-18 18:37:24 +00:00
|
|
|
|
to disable SSL.
|
|
|
|
|
|
2017-07-23 12:54:33 +00:00
|
|
|
|
The PostgreSQL storage backend does not automatically create the table. Here is
|
2017-03-08 02:47:23 +00:00
|
|
|
|
some sample SQL to create the schema and indexes.
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
CREATE TABLE vault_kv_store (
|
|
|
|
|
parent_path TEXT COLLATE "C" NOT NULL,
|
|
|
|
|
path TEXT COLLATE "C",
|
|
|
|
|
key TEXT COLLATE "C",
|
|
|
|
|
value BYTEA,
|
|
|
|
|
CONSTRAINT pkey PRIMARY KEY (path, key)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CREATE INDEX parent_path_idx ON vault_kv_store (parent_path);
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-10 16:48:42 +00:00
|
|
|
|
Store for HAEnabled backend
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
CREATE TABLE vault_ha_locks (
|
|
|
|
|
ha_key TEXT COLLATE "C" NOT NULL,
|
2020-01-18 00:18:09 +00:00
|
|
|
|
ha_identity TEXT COLLATE "C" NOT NULL,
|
|
|
|
|
ha_value TEXT COLLATE "C",
|
2019-05-10 16:48:42 +00:00
|
|
|
|
valid_until TIMESTAMP WITH TIME ZONE NOT NULL,
|
|
|
|
|
CONSTRAINT ha_key PRIMARY KEY (ha_key)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2017-04-04 16:29:54 +00:00
|
|
|
|
If you're using a version of PostgreSQL prior to 9.5, create the following function:
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
CREATE FUNCTION vault_kv_put(_parent_path TEXT, _path TEXT, _key TEXT, _value BYTEA) RETURNS VOID AS
|
|
|
|
|
$$
|
|
|
|
|
BEGIN
|
|
|
|
|
LOOP
|
|
|
|
|
-- first try to update the key
|
|
|
|
|
UPDATE vault_kv_store
|
|
|
|
|
SET (parent_path, path, key, value) = (_parent_path, _path, _key, _value)
|
|
|
|
|
WHERE _path = path AND key = _key;
|
|
|
|
|
IF found THEN
|
|
|
|
|
RETURN;
|
|
|
|
|
END IF;
|
|
|
|
|
-- not there, so try to insert the key
|
|
|
|
|
-- if someone else inserts the same key concurrently,
|
|
|
|
|
-- we could get a unique-key failure
|
|
|
|
|
BEGIN
|
|
|
|
|
INSERT INTO vault_kv_store (parent_path, path, key, value)
|
|
|
|
|
VALUES (_parent_path, _path, _key, _value);
|
|
|
|
|
RETURN;
|
|
|
|
|
EXCEPTION WHEN unique_violation THEN
|
|
|
|
|
-- Do nothing, and loop to try the UPDATE again.
|
|
|
|
|
END;
|
|
|
|
|
END LOOP;
|
|
|
|
|
END;
|
|
|
|
|
$$
|
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
```
|
|
|
|
|
|
2017-03-08 02:47:23 +00:00
|
|
|
|
## `postgresql` Parameters
|
|
|
|
|
|
|
|
|
|
- `connection_url` `(string: <required>)` – Specifies the connection string to
|
2019-12-03 21:48:38 +00:00
|
|
|
|
use to authenticate and connect to PostgreSQL. The connection URL can also be
|
|
|
|
|
set using the `VAULT_PG_CONNECTION_URL` environment variable. A full list of supported
|
2017-03-08 02:47:23 +00:00
|
|
|
|
parameters can be found in [the pq library documentation][pglib]. For example
|
|
|
|
|
connection string URLs, see the examples section below.
|
|
|
|
|
|
|
|
|
|
- `table` `(string: "vault_kv_store")` – Specifies the name of the table in
|
|
|
|
|
which to write Vault data. This table must already exist (Vault will not
|
|
|
|
|
attempt to create it).
|
|
|
|
|
|
2020-01-18 00:18:09 +00:00
|
|
|
|
- `max_idle_connections` `(int)` - Default not set. Sets the maximum number of
|
2019-07-02 22:03:56 +00:00
|
|
|
|
connections in the idle connection pool. See
|
2020-01-18 00:18:09 +00:00
|
|
|
|
[golang docs on SetMaxIdleConns][golang_setmaxidleconns] for more information.
|
2019-07-02 22:03:56 +00:00
|
|
|
|
Requires 1.2 or later.
|
|
|
|
|
|
2017-07-17 17:04:49 +00:00
|
|
|
|
- `max_parallel` `(string: "128")` – Specifies the maximum number of concurrent
|
|
|
|
|
requests to PostgreSQL.
|
|
|
|
|
|
2019-05-10 16:48:42 +00:00
|
|
|
|
- `ha_enabled` `(string: "true|false")` – Default not enabled, requires 9.5 or later.
|
|
|
|
|
|
2019-09-07 15:49:14 +00:00
|
|
|
|
- `ha_table` `(string: "vault_ha_locks")` – Specifies the name of the table to use
|
|
|
|
|
for storing high availability information. This table must already exist (Vault
|
|
|
|
|
will not attempt to create it).
|
|
|
|
|
|
2017-03-08 02:47:23 +00:00
|
|
|
|
## `postgresql` Examples
|
|
|
|
|
|
|
|
|
|
### Custom SSL Verification
|
|
|
|
|
|
2017-07-23 12:54:33 +00:00
|
|
|
|
This example shows connecting to a PostgreSQL cluster using full SSL
|
2017-03-08 02:47:23 +00:00
|
|
|
|
verification (recommended).
|
|
|
|
|
|
|
|
|
|
```hcl
|
2017-03-08 14:17:00 +00:00
|
|
|
|
storage "postgresql" {
|
2017-03-08 02:47:23 +00:00
|
|
|
|
connection_url = "postgres://user:pass@localhost:5432/database?sslmode=verify-full"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To disable SSL verification (not recommended), replace `verify-full` with
|
|
|
|
|
`disable`:
|
|
|
|
|
|
|
|
|
|
```hcl
|
2017-03-08 14:17:00 +00:00
|
|
|
|
storage "postgresql" {
|
2017-03-08 02:47:23 +00:00
|
|
|
|
connection_url = "postgres://user:pass@localhost:5432/database?sslmode=disable"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-01-18 00:18:09 +00:00
|
|
|
|
[golang_setmaxidleconns]: https://golang.org/pkg/database/sql/#DB.SetMaxIdleConns
|
2017-03-08 02:47:23 +00:00
|
|
|
|
[postgresql]: https://www.postgresql.org/
|
|
|
|
|
[pglib]: https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters
|