Using Azure Key Vault

Protect the data encryption key with a key stored in Azure Key Vault.

Configuration example

Create a key with Azure Key Vault:

az keyvault key create --vault-name pg-tde --name pg-tde-master-1

Use the az keyvault key command with the pg-tde-master-1 key to wrap and unwrap the data encryption key. You can provide these commands as environment variables or as initdb command-line arguments.

Via environment variable

PGDATAKEYWRAPCMD='az keyvault key encrypt --name pg-tde-master-1 --vault-name pg-tde --algorithm A256GCM --value @- --data-type plaintext --only-show-errors --output json | jq -r .result > "%p"'
PGDATAKEYUNWRAPCMD='az keyvault key decrypt --name pg-tde-master-1 --vault-name pg-tde --algorithm A256GCM --value @"%p" --data-type plaintext --only-show-errors --output json | jq -r .result'

Via command argument

initdb --data-encryption -D /var/lib/edb/as16/data \
  --key-wrap-command='az keyvault key encrypt --name pg-tde-master-1 --vault-name pg-tde --algorithm A256GCM --value @- --data-type plaintext --only-show-errors --output json | jq -r .result > "%p"' \
  --key-unwrap-command='az keyvault key decrypt --name pg-tde-master-1 --vault-name pg-tde --algorithm A256GCM --value @"%p" --data-type plaintext --only-show-errors --output json | jq -r .result'
Note

Shell commands with pipes, as in this example, have a drawback: a pipe's exit status reflects only its last command, so a failure earlier in the pipeline can go unreported. Postgres partly compensates by checking whether the wrap or unwrap command produced no output, but it's safer to make the command itself robust — for example, with the pipefail option available in some shells, or the mispipe command available on some operating systems. For anything more complex, put the command in an external shell script or program instead of defining it inline.


Could this page be better? Report a problem or suggest an addition!