# Keys and WAL encryption

> Separate database keys from the server key and preserve old key versions for recovery.

---

LLMS index: [llms.txt](/llms.txt)

---

## Where the keys live {#providers}

Relation keys are stored wrapped in the data directory. A database principal key from its provider unwraps them. The provider can be database-scoped or global. In the [file example](/docs/tde/tables/) the provider lives outside the data directory. For a deployment, choose a supported KMS such as Vault or KMIP and test its reachability and authentication from every intended recovery host. Record provider identity, key names and versions. Keep credentials, certificates and file keyrings recoverable independently of the database host. [Percona key management](/docs/tde/upstream/global-key-provider-configuration/)

## Encrypt the WAL stream {#wal}

WAL encryption applies to all databases, including ordinary heap tables. Establish the restore procedure on every backup and standby host before enabling it. On the test cluster, with the key directory from the previous page, configure a dedicated server principal key:

```sql
SELECT pg_tde_add_global_key_provider_file(
    'wal_provider', '/var/lib/mimus-keys/wal.per');
SELECT pg_tde_create_key_using_global_key_provider('server_key', 'wal_provider');
SELECT pg_tde_set_server_key_using_global_key_provider('server_key', 'wal_provider');
SELECT pg_tde_verify_server_key();
SELECT key_name, provider_name FROM pg_tde_server_key_info();
ALTER SYSTEM SET pg_tde.wal_encrypt = on;
```

Restart the server and run `SHOW pg_tde.wal_encrypt`. Expect `on`. Earlier WAL and backups retain their original requirements. Turning the switch off leaves historical encrypted segments in the recovery chain. For continuous archiving use Percona’s `pg_tde_archive_decrypt` and `pg_tde_restore_encrypt` as documented for the chosen archive system. Protect the decrypted archive independently.

## Rotation {#rotation}

Schedule rotation outside all backup jobs. Retain every principal/server key version needed from the oldest retained backup through its recovery targets. After rotation, take a fresh full backup and restore it on another host. A new key with the old name cannot decrypt data wrapped with the lost key. [Backup and recovery](/docs/tde/recovery/)

---

Backlinks:

- [Create encrypted permanent tables over TDS](/docs/mssql/encrypted-tables/)
- [PG TDE](/docs/tde/)
- [Enable TDE and encrypt a table](/docs/tde/tables/)
