Protect the data encryption key with a key stored in AWS Key Management Service (KMS).
Configuration example
Create a key with AWS Key Management Service:
aws kms create-key
aws kms create-alias --alias-name alias/pg-tde-master-1 --target-key-id "..."Use the aws kms command with the alias/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='aws kms encrypt --key-id alias/pg-tde-master-1 --plaintext fileb:///dev/stdin --output text --query CiphertextBlob | base64 -d > "%p"' PGDATAKEYUNWRAPCMD='aws kms decrypt --key-id alias/pg-tde-master-1 --ciphertext-blob fileb://"%p" --output text --query Plaintext | base64 -d'
Via command argument
initdb --data-encryption -D /var/lib/edb/as16/data \ --key-wrap-command='aws kms encrypt --key-id alias/pg-tde-master-1 --plaintext fileb:///dev/stdin --output text --query CiphertextBlob | base64 -d > "%p"' \ --key-unwrap-command='aws kms decrypt --key-id alias/pg-tde-master-1 --ciphertext-blob fileb://"%p" --output text --query Plaintext | base64 -d'
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.