Skip to content

This is the multi-page printable view of this section.

Return to the regular view of this page

Operations

Operational procedures for backup, restore, upgrades, enforcement, and multi-tenancy.

This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.

PG.CENTER edition · Upstream source

1 - Encryption enforcement

Require new tables to use encryption at the server, database, role, or session level.

This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.

For pg_tde, encryption enforcement ensures that only encrypted storage is allowed for specific operations, tables, or the entire database. It prevents the accidental creation of unencrypted tables or indexes in environments where encryption is required for compliance, security, or policy enforcement.

What does enforcement do?

When enabled, encryption enforcement:

  • Prevents creation of unencrypted tables or indexes
  • Enforces consistent encryption usage across tenants, databases, or users
  • Can be scoped globally, per database, or per role

Enforce encryption usage

Use the following techniques to enforce the secure use of pg_tde.

1. Enforce encryption across the server

To enforce encryption cluster-wide, set the pg_tde.enforce_encryption variable in postgresql.conf:

pg_tde.enforce_encryption = on
Note

Only superusers can set or change this variable.

This ensures that no user, including superusers, can create unencrypted tables. Superusers can however explicitly override the variable in their session.

2. Enforce encryption for a specific database

To apply encryption enforcement only within a specific database, run:

ALTER DATABASE example_db SET pg_tde.enforce_encryption = on;

This ensures encryption is enforced only when connected to that database.

3. Enforce encryption for a specific user

You can also enforce encryption on a per-user basis, run:

ALTER USER example_user SET pg_tde.enforce_encryption = on;

This ensures that the user example_user cannot create unencrypted tables, regardless of which database they connect to.

Override enforcement for trusted sessions

Superusers can override the variable at the session level:

SET pg_tde.enforce_encryption = off;

This allows temporary creation of unencrypted tables in special cases, such as:

  • Loading trusted, public reference datasets
  • Benchmarking and test environments
  • Migration staging before re-encryption
Note

While superusers can disable enforcement in their session, they must do so explicitly. Enforcement defaults remain active to protect from accidental misconfiguration.

PG.CENTER edition · Upstream source

2 - Configure multi-tenancy

Separate database keys and privileges for tenants sharing a PostgreSQL cluster.

This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.

The steps below describe how to set up multi-tenancy with pg_tde. Multi-tenancy allows you to encrypt different databases with different keys. This provides granular control over data and enables you to introduce different security policies and access controls for each database so that only authorized users of specific databases have access to the data.

If you don’t need multi-tenancy, use the global key provider. See the configuration steps from the Configure pg_tde section.

For how to enable WAL encryption, refer to the Configure WAL Encryption section.

Considerations

You can use external key providers to manage encryption keys. The recommended approach is to use the Key Management Store (KMS). For more information, see Configure Key Management (KMS) .

Note

If no error is reported when running the commands below, the operation completed successfully.

Enable extension

Load the pg_tde at startup time. The extension requires additional shared memory; therefore, add the pg_tde value for the shared_preload_libraries parameter and restart the postgresql cluster.

  1. Use the ALTER SYSTEM command from psql terminal to modify the shared_preload_libraries parameter. This requires superuser privileges.

    ALTER SYSTEM SET shared_preload_libraries = 'pg_tde';
  2. Start or restart the postgresql cluster to apply the changes.

    • On Debian and Ubuntu:

      sudo systemctl restart postgresql-17
    • On RHEL and derivatives

      sudo systemctl restart postgresql-17
  3. Create the extension using the CREATE EXTENSION command. You must have the privileges of a superuser or a database owner to use this command. Connect to psql as a superuser for a database and run the following command:

    CREATE EXTENSION pg_tde;

    The pg_tde extension is created for the currently used database. To enable data encryption in other databases, you must explicitly run the CREATE EXTENSION command against them.

    Tip

    You can have the pg_tde extension automatically enabled for every newly created database. Modify the template template1 database as follows:

    psql -d template1 -c 'CREATE EXTENSION pg_tde;'

Key provider configuration

You must do these steps for every database where you have created the extension. For more information on configurations, please see the Configure Key Management (KMS) topic.

  1. Set up a key provider.

     **Warning:** This example is for testing purposes only:
    
     ```sql
     SELECT pg_tde_add_database_key_provider_file(
         'file-keyring', 
         '/tmp/pg_tde_test_local_keyring.per'
     );
     ```
    
  2. Create a key

    SELECT pg_tde_create_key_using_database_key_provider(
        'name-of-the-key', 
        'provider-name'
    );

    where:

    • name-of-the-key is the name of the principal key. You will use this name to identify the key.
    • provider-name is the name of the key provider you added before. The principal key is associated with this provider and it is the location where it is stored and fetched from.

    Warning: This example is for testing purposes only:

    SELECT pg_tde_create_key_using_database_key_provider(
        'test-db-master-key', 
        'file-vault'
    );
    Note

    The key is auto-generated.

  3. Use the key as principal key

    SELECT pg_tde_set_key_using_database_key_provider(
        'name-of-the-key', 
        'provider-name'
    );

    where:

    • name-of-the-key is the name of the principal key. You will use this name to identify the key.
    • provider-name is the name of the key provider you added before. The principal key will be associated with this provider.

    Warning: This example is for testing purposes only:

    SELECT pg_tde_set_key_using_database_key_provider(
        'test-db-master-key',
        'file-vault'
    );

PG.CENTER edition · Upstream source

3 - Backup with WAL encryption enabled

Back up an encrypted cluster and handle WAL with pgBackRest and pg_tde tools.

For Mimus, choose the backup client by WAL key history and use the client paired with the kernel. See the tested physical backup and restore sequence. Mimus procedure

Danger

Do not rotate encryption keys while a backup is running. This may result in an inconsistent backup and restore failure. This applies to all backup tools.

For more details, see Limitations of pg_tde.

To create a backup with WAL encryption enabled:

  1. Copy the pg_tde directory from the source server’s data directory, for example /var/lib/postgresql/data/pg_tde/, including the wal_keys and 1664_providers files, to the backup destination directory where pg_tde_basebackup will write the backup.

    Also copy any external files referenced by your providers configuration (such as certificate or key files) into the same relative paths under the backup destination, so that they are located and validated by pg_tde_basebackup -E.

  2. Run:

    pg_tde_basebackup -D /path/to/backup -E [aes_128|aes_256]

    Where:

    • -D /path/to/backup specifies the backup location where you have to copy pg_tde.
    • -E (or --encrypt-wal) enables WAL encryption and validates that the copied pg_tde and provider files are present and that the server key is accessible (required). The -E option accepts a cipher argument. The currently supported values are aes_128 and aes_256. If -E is not specified, pg_tde_basebackup attempts to determine the cipher from the server’s key. If no cipher can be determined, pg_tde_basebackup defaults to aes_128.
Note
  • The -E flag only works with the -X stream option (default). It is not compatible with -X none or -X fetch. For more information, see the backup method compatibility with WAL encryption.
  • The -E flag is only supported with the plain output format (-F p). It cannot be used with the tar output format (-F t).

Key rotation during backups

Danger

Do not rotate encryption keys while a backup is in progress. This applies to all backup tools.

Backups created during key rotation may fail to start during WAL replay and may lead to corruption of encrypted data (tables, indexes, and other relations).

For example, you may see errors such as:

FATAL: failed to retrieve principal key "database_keyXXXX" from key provider "providerYYYY"
CONTEXT: WAL redo at ... ROTATE_PRINCIPAL_KEY ...

To ensure standby recoverability, plan key rotations outside backup windows or take a new full backup after rotation completes.

Restore a backup created with WAL encryption

When you want to restore a backup created with pg_tde_basebackup -E:

  1. Ensure all external files referenced by your providers configuration (such as certificates or key files) are also present and accessible at the same relative paths.
  2. Start PostgreSQL with the restored data directory.

Backup method compatibility with WAL encryption

Tar format (-F t):

  • Works with -X fetch.
  • Does not support -X stream when WAL encryption is enabled. Using pg_tde_basebackup -F t -X stream will create a broken replica.

Streaming mode (-X stream):

  • Must specify -E (--encrypt-wal).
  • Without -E, backups may contain decrypted WAL while wal_encryption=on remains in postgresql.conf and pg_tde/wal_keys are copied. This leads to startup failures and compromised data in the backup.

Fetch mode (-X fetch):

  • Compatible with encrypted WAL without requiring any additional flags.

None (-X none):

  • Excludes WAL from the backup and is unaffected by WAL encryption.
Note

If the source server has pg_tde/wal_keys, running pg_tde_basebackup with -X none or -X fetch produces warnings such as:

pg_tde_basebackup: warning: the source has WAL keys, but no WAL encryption configured for the target backups
pg_tde_basebackup: detail: This may lead to exposed data and broken backup
pg_tde_basebackup: hint: Run pg_tde_basebackup with -E to encrypt streamed WAL

You can safely ignore the warnings with -X none or -X fetch, since no WAL streaming occurs.

PG.CENTER edition · Upstream source

4 - Restore encrypted backups

Restore encrypted data while retaining access to keys and required metadata.

For Mimus, restore the matching kernel, startup libraries, provider metadata and historical keys along with the backup. Mimus procedure

To restore an encrypted backup created with an earlier key, ensure the key is still available in your Key Management System (KMS). The backup remains encrypted on disk, pg_tde uses the correct key to transparently access the data.

How pg_tde uses old keys to load backups

  • KMS: stores the principal key(s) that were active when the backup was made.
  • Encrypted backup: contains data encrypted with the key available at that time.
  • Current server: pg_tde automatically loads the backup if the key is accessible through the KMS.

The table (internal) keys are stored within the backup. At runtime, pg_tde retrieves the principal key(s) from the configured KMS and uses them to decrypt the internal keys, enabling access to the encrypted table data.

Note

If the required key(s) get deleted or are missing, the backup cannot be read.

If the key(s) still exists but the KMS configuration has changed, you can use pg_tde_change_key_provider to update the configuration.

However, if the key(s) got deleted from all accessible KMS sources, the encrypted backup is unrecoverable.

PG.CENTER edition · Upstream source

5 - Upgrade pg_tde

Plan extension upgrades and major PostgreSQL upgrades for encrypted clusters.

Percona’s package upgrade steps apply to Percona PostgreSQL. For a major upgrade of a TDE-marked Mimus cluster use logical dump and restore.

The version of pg_tde available to you depends on your version of Percona Distribution for PostgreSQL.

To use a newer pg_tde release, first ensure that your Percona Distribution for PostgreSQL version includes it.

This document describes upgrading the pg_tde extension within the same PostgreSQL major version. For PostgreSQL major version upgrades on clusters that use pg_tde, use pg_tde_upgrade.

Before you start

Note

pg_tde 2.2.0 is not compatible with Percona Distribution for PostgreSQL older than 17.10 or 18.4. Upgrade your distribution first before upgrading pg_tde.

Warning

Using pg_upgrade on an encrypted cluster is not supported and will result in data corruption. The server may start successfully but queries against encrypted tables will fail.

Procedure

  1. Take a full backup before upgrading.

  2. Upgrade the pg_tde package via your package manager.

  3. Restart the postgresql service.

  4. Connect to each database where pg_tde is installed and run:

    ALTER EXTENSION pg_tde UPDATE;
    Note

    The catalog version of pg_tde may not change between patch releases. You can run ALTER EXTENSION pg_tde UPDATE safely regardless.

  5. Verify the upgrade:

SELECT extname, extversion FROM pg_extension WHERE extname = 'pg_tde';
SELECT pg_tde_version();

PG.CENTER edition · Upstream source

6 - Remove encryption from an encrypted table

Rewrite a tde_heap table with the standard heap access method.

This is the Percona pg_tde 2.3.0 manual. For the paired build, backup tools and major upgrades, see PG TDE on Mimus.

Method 1. Change the access method

If you encrypted a table with the tde_heap access method and need to remove the encryption from it, run the following command against the desired table (mytable in the example below):

ALTER TABLE mytable SET ACCESS METHOD heap;

Note that the SET ACCESS METHOD command drops hint bits and this may affect performance. Running a plain SELECT count(*) or VACUUM command on the entire table will check every tuple for visibility and set its hint bits. Therefore, after executing the ALTER TABLE command, run a simple count(*) on your tables:

SELECT count(*) FROM mytable;

Check that the table is not encrypted:

SELECT pg_tde_is_encrypted('mytable');

The output returns f meaning that the table is no longer encrypted.

Method 2. Create a new not encrypted table on the base of the encrypted one

Alternatively, you can create a new not encrypted table with the same structure and data as the initial table. For example, the original encrypted table is EncryptedCustomers. Use the following command to create a new table Customers:

CREATE TABLE Customers AS SELECT * FROM EncryptedCustomers;

The new table Customers inherits the structure and the data from EncryptedCustomers.

(Optional) If you no longer need the EncryptedCustomers table, you can delete it.

DROP TABLE EncryptedCustomers;

PG.CENTER edition · Upstream source

7 - Uninstall pg_tde

Remove encryption dependencies before uninstalling the extension and packages.

Mimus records TDE in the data directory. Retain the matching preload, tools and historical keys for recovery, including after dropping the SQL extension. To retire the dependency, migrate into a fresh cluster. Mimus procedure

If you no longer wish to use Transparent Data Encryption (TDE) in your deployment, you can remove the pg_tde extension.

To proceed, you must have one of the following privileges:

  • Superuser privileges (to remove the extension globally), or
  • Database owner privileges (to remove it from a specific database only)

To uninstall pg_tde, follow the steps below.

Step 1. Remove pg_tde from all databases

Before uninstalling, you must remove the extension from every database where it is loaded. If pg_tde was installed in templates, new databases inherit it. You must drop it from the templates first or future restores/creates will break.

a. Rewrite encrypted tables:

To rewrite an encrypted table back to its default storage method:

ALTER TABLE <table_name> SET ACCESS METHOD heap;

b. Remove the extension once all encrypted tables have been rewritten or dropped:

DROP EXTENSION pg_tde;
Note

If there are any encrypted objects that were not previously decrypted or deleted, this command will fail and you have to follow the steps above for these objects.

Repeat step 1 for every database in your cluster.

Step 2. Turn off WAL encryption

If you are using WAL encryption, you need to turn it off before you uninstall the pg_tde library:

a. Run:

ALTER SYSTEM SET pg_tde.wal_encrypt = off;

b. Restart the PostgreSQL cluster to apply the changes:

  • On Debian and Ubuntu:
sudo systemctl restart postgresql
  • On RHEL and derivatives:
sudo systemctl restart postgresql-(pg-version)

Step 3. Uninstall the pg_tde shared library

Warning

This process removes the extension, but does not decrypt data automatically. Only uninstall the shared library after all encrypted data has been removed or decrypted and WAL encryption has been disabled.

Note

Encrypted WAL pages will not be decrypted, so any postgres cluster needing to read them will need the pg_tde library loaded, and the WAL encryption keys available and in use.

At this point, the shared library is still loaded but no longer active. To fully uninstall pg_tde, complete the steps below.

a. Run SHOW shared_preload_libraries to view the current configuration of preloaded libraries.

For example:

postgres=# SHOW shared_preload_libraries;
        shared_preload_libraries
-----------------------------------------
pg_stat_statements,pg_tde,auto_explain
(1 row)

postgres=#

b. Remove pg_tde from the list and apply the new setting using ALTER SYSTEM SET shared_preload_libraries=<your list of libraries>.

For example:

postgres=# ALTER SYSTEM SET shared_preload_libraries=pg_stat_statements,auto_explain;
ALTER SYSTEM
postgres=#
Note

Your list of libraries will most likely be different than the above example. Take care not to remove other required extensions. Only delete pg_tde from the list.

If pg_tde is the only shared library in the list, and it was set via postgresql.conf you cannot disable it using the ALTER SYSTEM SET ... command. Instead:

  1. Remove the shared_preload_libraries line from postgresql.conf
  2. Run ALTER SYSTEM RESET shared_preload_libraries;

c. Restart the postgresql cluster to apply the changes:

  • On Debian and Ubuntu:

    sudo systemctl restart postgresql
  • On RHEL and derivatives:

    sudo systemctl restart postgresql-(pg-version)
    Note

    The service name may differ depending on your distribution and PostgreSQL version.nod

At this point it is safe to remove any configuration related to pg_tde from postgresql.conf and postgresql.auto.conf. Look for any configuration parameters prefixed with pg_tde. and remove or comment them out, as needed.

Troubleshooting: PANIC checkpoint not found on restart

This can happen if WAL encryption was not properly disabled before removing pg_tde from shared_preload_libraries, when the PostgreSQL server was not restarted after disabling WAL encryption (see step 3.c).

You might see this when restarting the PostgreSQL cluster:

2025-04-01 17:12:50.607 CEST [496385] PANIC:  could not locate a valid checkpoint record at 0/17B2580

This error means PostgreSQL is trying to read encrypted WAL without pg_tde loaded.

To resolve it follow these steps:

  1. Re-add pg_tde to shared_preload_libraries
  2. Restart the PostgreSQL cluster
  3. Follow the instructions for turning off WAL encryption before uninstalling the shared library again
Note

Two restarts are required to uninstall properly if WAL encryption was enabled:

  • First to disable WAL encryption
  • Second to remove the pg_tde library

PG.CENTER edition · Upstream source