# pg_tde_restore_encrypt

> Wrap a restore command and encrypt the retrieved WAL before PostgreSQL reads it.

---

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

---

> This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see [PG TDE on Mimus](/docs/tde/).

> [!NOTE] Editorial correction
> The placeholder descriptions now match the tool’s built-in help. See [site errata](/docs/tde/upstream/site/errata/).

The `pg_tde_restore_encrypt` tool wraps a normal restore command from the WAL archive and writes them to disk in a format compatible with `pg_tde`.

> [!NOTE]
>
> This command is often use together with [pg_tde_archive_decrypt](/docs/tde/upstream/command-line-tools/pg-tde-archive-decrypt/).

## How it works {#how-it-works}

1. Replaces `%f` and `%p` in the restore command with the WAL file name and temporary file path (in `/dev/shm`)
2. Runs the restore command to fetch the unencrypted WAL from the archive and write it to the temp file
3. Encrypts the temp file and writes it to the destination path in PostgreSQL’s data directory

## Usage {#usage}

```bash
pg_tde_restore_encrypt [OPTION]
pg_tde_restore_encrypt SOURCE-NAME DEST-PATH RESTORE-COMMAND
```

## Parameter descriptions {#parameter-descriptions}

* `SOURCE-NAME`: name of the WAL file to retrieve from the archive
* `DEST-PATH`: path where the encrypted WAL file should be written
* `RESTORE-COMMAND`: restore command to wrap; `%f` and `%p` are replaced with the WAL file name and path to write the unencrypted WAL, respectively

## Options {#options}

* `-V, --version`: show version information, then exit
* `-?, --help`: show help information, then exit

> [!NOTE]
>
> Any `%f` or `%p` parameter in `RESTORE-COMMAND` has to be escaped as `%%f` or `%%p` respectively if used as `restore_command` in `postgresql.conf`.

## Examples {#examples}

### Using `cp` {#using-cp}

```ini
restore_command='pg_tde_restore_encrypt %f %p "cp /mnt/server/archivedir/%%f %%p"'
```

### Using `PgBackRest` {#using-pgbackrest}

```ini
restore_command='pg_tde_restore_encrypt %f %p "pgbackrest --stanza=your_stanza archive-get %%f \"%%p\""'
```

> [!WARNING] Required configuration
>
> A deployment with WAL encryption is not supported unless `archive_command` uses [`pg_tde_archive_decrypt`](/docs/tde/upstream/command-line-tools/pg-tde-archive-decrypt/) and `restore_command` uses `pg_tde_restore_encrypt`.
>
> Disable the following pgBackRest features in the applicable pgBackRest configuration section:
>
> ```ini
> archive-async=n
> archive-header-check=n
> checksum-page=n
> ```

[PG.CENTER edition](https://github.com/pgsty/doc.pg.center/blob/4410c32dc9186a22859008303f474b72a779f583/content/percona/command-line-tools/pg-tde-restore-encrypt.md) · [Upstream source](https://github.com/percona/pg_tde/blob/5dd7a9e4e3599d88546f5de67ff0f4ef3fe51a9a/documentation/docs/command-line-tools/pg-tde-restore-encrypt.md)

---

Backlinks:

- [Tool overview](/docs/tde/upstream/command-line-tools/cli-tools/)
- [Archive WAL](/docs/tde/upstream/command-line-tools/pg-tde-archive-decrypt/)
- [Documentation errata](/docs/tde/upstream/site/errata/)
- [WAL encryption](/docs/tde/upstream/wal-encryption/)
