Skip to content

Transparent Data Encryption (TDE) with PostgreSQL

Preamble

Transparent Data Encryption (TDE) is, surprisingly, NOT supported by the open source version of PostgreSQL.

postgresql_tde01
postgresql_tde01

Some software providers do provide this feature with their proprietary editions like:

But all of them are not free and licenses must be acquired ! Again in my opinion this a bit breaking the open source spirit of PostgreSQL. They take base software from the community, they had a piece of proprietary code and they sell it… The only free alternative is pg_tde from Percona, but they claim that the extension is not yet ready for production…

Azure Database for PostgreSQL flexible server does not support TDE. Instead Microsoft rely on Azure Storage encryption for data at rest, so not a pure PostgreSQL native Transparent Data Encryption extension… AWS Aurora PostgreSQL has the same philosophy using Amazon RDS encryption, making a pure data disk encryption…

They have a similar screenshot to configure the disk encryption key, for Azure. Microsoft claim it is the same as TDE for SQL Server or Oracle but I disagree. TDE forbid access to datafiles if attacked gets an access to your system, it protects only against stolen disks:

postgresql_tde02
postgresql_tde02

for AWS:

postgresql_tde03
postgresql_tde03

This testing has been done using PostgreSQL Community Edition 17.2 as well as the container build by Percona running in Podman 5.3.0 on my windows desktop. My test server is virtual machine under VirtualBox running Oracle Linux Server release 9.5 with 4 cores and 4GB of memory. Pg_tde tested version is 1.0-beta2, again try to contribute to the project to see it going live…

PostgreSQL without Transparent Data Encryption (TDE)

To test few things I have decided to create a tablespace with (in production, as stated, do not create it in PGDATA directory):

postgres=# create tablespace tbs01 location '/postgres/17/data/base';
WARNING:  tablespace location should not be inside the data directory
CREATE TABLESPACE

postgres=# \db
              List of tablespaces
    Name    |  Owner   |        Location
------------+----------+------------------------
 pg_default | postgres |
 pg_global  | postgres |
 tbs01      | postgres | /postgres/17/data/base
(3 rows)

postgres=# \db+
                                           List of tablespaces
    Name    |  Owner   |        Location        | Access privileges | Options |    Size    | Description
------------+----------+------------------------+-------------------+---------+------------+-------------
 pg_default | postgres |                        |                   |         | 29 MB      |
 pg_global  | postgres |                        |                   |         | 565 kB     |
 tbs01      | postgres | /postgres/17/data/base |                   |         | 8288 bytes |
(3 rows)

There is also a directory creation with symbolic links to your tablespaces files:

[postgres@server01 ~]$ ll $PGDATA/pg_tblspc
total 0
lrwxrwxrwx 1 postgres dba 22 Dec 13 12:48 16393 -> /postgres/17/data/base

Once you have created an object in tbs01 schme you will be able to see your tablespace in system views (strangely not before):

postgres=# select pgt.*, pgd.datname
           from (select * ,pg_tablespace_location(oid), pg_tablespace_databases(oid) as database_oid from pg_tablespace) pgt,
                pg_database pgd
           where pgt.database_oid=pgd.oid
           and pgt.spcname='tbs01';
  oid  | spcname | spcowner | spcacl | spcoptions | pg_tablespace_location | database_oid | datname
-------+---------+----------+--------+------------+------------------------+--------------+----------
 16393 | tbs01   |       10 |        |            | /postgres/17/data/base |        16384 | yjaquier
(1 row)

Create a test table in tde schema and insert two rows into it:

yjaquier=# create schema tde;
CREATE SCHEMA
yjaquier=# create table tde.test01(id int, descr varchar(20)) tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test01 values(1, 'One');
INSERT 0 1
yjaquier=# insert into tde.test01 values(2, 'Two');
INSERT 0 1
yjaquier=# \dt tde.*
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 tde    | test01 | table | postgres
(1 row)

You can get the exact data file name where is store your table with:

yjaquier=# select pg_relation_filepath('tde.test01');
pg_relation_filepath
---------------------------------------------
pg_tblspc/16397/PG_17_202406281/16384/16399
(1 row)

As expected you can read in clear text the information of the tablespace files:

[postgres@server01 ~]$ ll $PGDATA/pg_tblspc/16397/PG_17_202406281/16384/16399
-rw------- 1 postgres dba 8192 Jan 13 17:04 /postgres/17/data/pg_tblspc/16397/PG_17_202406281/16384/16399
[postgres@server01 ~]$ xxd $PGDATA/pg_tblspc/16397/PG_17_202406281/16384/16399 | egrep -e 'One' -e 'Two'
00001fd0: 0200 0200 0208 1800 0200 0000 0954 776f  .............Two
00001ff0: 0100 0200 0208 1800 0100 0000 094f 6e65  .............One
[postgres@server01 ~]$ strings $PGDATA/pg_tblspc/16397/PG_17_202406281/16384/16399
        Two
        One

PostgreSQL with Transparent Data Encryption (TDE)

To ease all the steps it is strongly suggested to set below in your postgres account profile (I use 5433 as a listening port):

PGDATA=/postgres/17/data
export PGDATA
alias psql="psql --port=5433"
alias pg_start="pg_ctl -D ${PGDATA} -l logfile start"
alias pg_restart="pg_ctl -D ${PGDATA} -l logfile restart"
alias pg_stop="pg_ctl -D ${PGDATA} -l logfile stop"

I have downloaded the pg_tde source from GitHub and tried to compile them:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security
-fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions
-fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection
-fcf-protection -fPIC -fvisibility=hidden -I/home/postgres/pg_tde-main/src/include -I/home/postgres/pg_tde-main/src/libkmip/libkmip/include -I/home/postgres/pg_tde-main/src17/include -I. -I./ -I/usr/pgsql-17/include/server
-I/usr/pgsql-17/include/internal  -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include  -c -o src/keyring/keyring_kmip.o src/keyring/keyring_kmip.c
src/keyring/keyring_kmip.c:15:10: fatal error: kmip.h: No such file or directory
 #include <kmip.c>
          ^~~~~~~~
compilation terminated.
make: *** [<builtin>: src/keyring/keyring_kmip.o] Error 1

I have found that this header file can be found in libkmip project. It’s an open source implementation of the Key Management Interoperability Protocol (KMIP). Compile it with:

[root@server01 libkmip-master]# make
mkdir -p obj
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c src/kmip.c -o obj/kmip.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c src/kmip_memset.c -o obj/kmip_memset.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c src/kmip_io.c -o obj/kmip_io.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c src/kmip_bio.c -o obj/kmip_bio.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -fPIC -c src/kmip.c -o obj/kmip.lo
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -fPIC -c src/kmip_memset.c -o obj/kmip_memset.lo
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -fPIC -c src/kmip_io.c -o obj/kmip_io.lo
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -fPIC -c src/kmip_bio.c -o obj/kmip_bio.lo
mkdir -p bin
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c demos/demo_get.c -o obj/demo_get.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c demos/demo_create.c -o obj/demo_create.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c demos/demo_destroy.c -o obj/demo_destroy.o
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c demos/demo_query.c -o obj/demo_query.o
cc -L/usr/local/lib64 -L/usr/local/lib -o bin/demo_get obj/demo_get.o obj/kmip.o obj/kmip_memset.o obj/kmip_io.o obj/kmip_bio.o -lssl -lcrypto
cc -L/usr/local/lib64 -L/usr/local/lib -o bin/demo_create obj/demo_create.o obj/kmip.o obj/kmip_memset.o obj/kmip_io.o obj/kmip_bio.o -lssl -lcrypto
cc -L/usr/local/lib64 -L/usr/local/lib -o bin/demo_destroy obj/demo_destroy.o obj/kmip.o obj/kmip_memset.o obj/kmip_io.o obj/kmip_bio.o -lssl -lcrypto
cc -L/usr/local/lib64 -L/usr/local/lib -o bin/demo_query obj/demo_query.o obj/kmip.o obj/kmip_memset.o obj/kmip_io.o obj/kmip_bio.o -lssl -lcrypto
cc -std=c11 -pedantic -g3 -Wall -Wextra -Iinclude -c tests/tests.c -o obj/tests.o
cc -L/usr/local/lib64 -L/usr/local/lib -o bin/tests obj/tests.o obj/kmip.o obj/kmip_io.o obj/kmip_memset.o
mkdir -p lib
cc -std=c11 -pedantic -g3 -Wall -Wextra -shared -Wl,-soname,libkmip.so.0 -o lib/libkmip.so.0.2.1 obj/kmip.lo obj/kmip_memset.lo obj/kmip_io.lo obj/kmip_bio.lo -lssl -lcrypto
cc -std=c11 -pedantic -g3 -Wall -Wextra -shared -Wl,-soname,libkmip-core.so.0 -o lib/libkmip-core.so.0.2.1 obj/kmip.lo obj/kmip_memset.lo
ar csrv lib/libkmip.a obj/kmip.o obj/kmip_memset.o obj/kmip_io.o obj/kmip_bio.o
a - obj/kmip.o
a - obj/kmip_memset.o
a - obj/kmip_io.o
a - obj/kmip_bio.o
ar csrv lib/libkmip-core.a obj/kmip.o obj/kmip_memset.o
a - obj/kmip.o
a - obj/kmip_memset.o
ar csrv lib/libkmip-sgx.a obj/kmip.lo obj/kmip_memset.lo
a - obj/kmip.lo
a - obj/kmip_memset.lo

install it with:

[root@server01 libkmip-master]# make install
mkdir -p /usr/local/bin/kmip
mkdir -p /usr/local/include/kmip
mkdir -p /usr/local/lib
mkdir -p /usr/local/src/kmip
mkdir -p /usr/local/share/doc/kmip/src
cp bin/demo_create /usr/local/bin/kmip
cp bin/demo_get /usr/local/bin/kmip
cp bin/demo_destroy /usr/local/bin/kmip
cp bin/demo_query /usr/local/bin/kmip
cp -r docs/source/. /usr/local/share/doc/kmip/src
cp src/*.c /usr/local/src/kmip
cp include/*.h /usr/local/include/kmip
cp lib/* /usr/local/lib
ln -sf libkmip.so.0.2.1 /usr/local/lib/libkmip.so
ln -sf libkmip.so.0.2.1 /usr/local/lib/libkmip.so.0
ln -sf libkmip-core.so.0.2.1 /usr/local/lib/libkmip-core.so
ln -sf libkmip-core.so.0.2.1 /usr/local/lib/libkmip-core.so.0
[root@server01 libkmip-master]# ll /usr/local/include/kmip
total 84
-rw-r--r-- 1 root root  1576 Jan 21 12:32 kmip_bio.h
-rw-r--r-- 1 root root 68108 Jan 21 12:32 kmip.h
-rw-r--r-- 1 root root  5580 Jan 21 12:32 kmip_io.h
-rw-r--r-- 1 root root   416 Jan 21 12:32 kmip_memset.h

Then retried the compilation of pg_tde with:

[postgres@server01 pg_tde-main]$ export C_INCLUDE_PATH=/usr/local/include/kmip
[postgres@server01 pg_tde-main]$ make USE_PGXS=1
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security
-fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong
-grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC -fvisibility=hidden
-I/home/postgres/pg_tde-main/src/include -I/home/postgres/pg_tde-main/src/libkmip/libkmip/include -I/home/postgres/pg_tde-main/src17/include -I. -I./ -I/usr/pgsql-17/include/server -I/usr/pgsql-17/include/internal  -D_GNU_SOURCE
-I/usr/include/libxml2  -I/usr/include  -c -o src/keyring/keyring_kmip.o src/keyring/keyring_kmip.c
src/keyring/keyring_kmip.c:17:10: fatal error: kmip_locate.h: No such file or directory
 #include <kmip_locate.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: src/keyring/keyring_kmip.o] Error 1

I have not been able to find any source for this kmip_locate.h header file…

Downloading a compiled version of pg_tde

I did not see it initially but you can download a complied version of pg_tde by clicking on the latest button on GitHub page:

postgresql_tde04
postgresql_tde04

Download the version for your server (pgtde-pgdg17.tar.gzfor me), unzip it and copy the files to your PostgreSQL 17 binary directory (default installation for me):

[postgres@server01 ~]$ mkdir pg_tde
[postgres@server01 ~]$ cd pg_tde
[postgres@server01 pg_tde]$ gtar xvf ../pgtde-pgdg17.tar.gz
./
./usr/
./usr/lib/
./usr/lib/postgresql/
./usr/lib/postgresql/17/
./usr/lib/postgresql/17/lib/
./usr/lib/postgresql/17/lib/pg_tde.so
./share/
./share/postgresql/
./share/postgresql/17/
./share/postgresql/17/extension/
./share/postgresql/17/extension/pg_tde.control
./share/postgresql/17/extension/pg_tde--1.0-beta2.sql
./DEBIAN/
./DEBIAN/control
[root@server01 lib]# pwd
/home/postgres/pg_tde/usr/lib/postgresql/17/lib
[root@server01 lib]# cp pg_tde.so /usr/pgsql-17/lib/
[root@server01 lib]# chmod 755 /usr/pgsql-17/lib/pg_tde.so
[root@server01 extension]# pwd
/home/postgres/pg_tde/share/postgresql/17/extension
[root@server01 extension]# cp * /usr/pgsql-17/share/extension/
[root@server01 extension]# chmod 644 /usr/pgsql-17/share/extension/pg_tde*

Alter your PostgreSQL cluster to load the pg_tde library:

postgres=# alter system set shared_preload_libraries = 'pg_tde';
ALTER SYSTEM

Restart PostgreSQL cluster but it failed for:

2025-01-21 18:21:55.416 CET [2671532] FATAL:  could not load library "/usr/pgsql-17/lib/pg_tde.so": libcrypto.so.3: cannot open shared object file: No such file or directory
2025-01-21 18:21:55.416 CET [2671532] LOG:  database system is shut down

This library is included in below package:

[root@server01 ~]# dnf repoquery -l openssl3-libs.x86_64 | grep libcrypto.so.3
Red Hat Enterprise Linux 8 for x86_64 - BaseOS   60 kB/s | 4.1 kB     00:00
TSM                                              54 kB/s | 2.0 kB     00:00
Red Hat Enterprise Linux 8 for x86_64 - AppStre  66 kB/s | 4.5 kB     00:00
STMtools                                         38 kB/s | 2.0 kB     00:00
Red Hat CodeReady Linux Builder for RHEL 8 x86_  49 kB/s | 4.5 kB     00:00
/usr/lib64/libcrypto.so.3
/usr/lib64/libcrypto.so.3.2.2

Install it with:

[root@server01 ~]# dnf install openssl3-libs.x86_64

But this time it failed for:

2025-01-22 11:24:25.360 CET [3044176] FATAL:  could not load library "/usr/pgsql-17/lib/pg_tde.so": /lib64/libc.so.6: version `GLIBC_2.33' not found (required by /usr/pgsql-17/lib/pg_tde.so)
2025-01-22 11:24:25.360 CET [3044176] LOG:  database system is shut down

I could have seen it before with ldd command:

[postgres@server01 log]$ ldd /usr/pgsql-17/lib/pg_tde.so
/usr/pgsql-17/lib/pg_tde.so: /lib64/libcurl.so.4: no version information available (required by /usr/pgsql-17/lib/pg_tde.so)
/usr/pgsql-17/lib/pg_tde.so: /lib64/libc.so.6: version `GLIBC_2.33' not found (required by /usr/pgsql-17/lib/pg_tde.so)
.
.
.

Problem is that GLibC 2.33 is not available on RedHat 8 as the only available version is 2.28.

I have also tried with pgtde-pgdg16.tar.gz for PostgreSQL 16 but the dependency on Glibc 2.33 is the same:

[postgres@server01 lib]$ pwd
/home/postgres/pg_tde/usr/lib/postgresql/16/lib
[postgres@server01 lib]$ ldd pg_tde.so
./pg_tde.so: /lib64/libcurl.so.4: no version information available (required by ./pg_tde.so)
./pg_tde.so: /lib64/libc.so.6: version `GLIBC_2.33' not found (required by ./pg_tde.so)
        linux-vdso.so.1 (0x00007ffd4a4e8000)
        libcurl.so.4 => /lib64/libcurl.so.4 (0x00007fa94d35e000)
.
.
.

So what’s apparently not said is the requirement to have RedHat 9 or a more modern OS with latest Glibc version. I’m wondering if on RedHat 9 I could use Glibc 2.34 instead of 2.33…

I gave a try on Oracle Linux 9.5 and it is working perfectly with Glibc 2.34 (the warning on libcurl version had no impact):

[root@server01 ~]# ldd /usr/pgsql-17/lib/pg_tde.so
/usr/pgsql-17/lib/pg_tde.so: /lib64/libcurl.so.4: no version information available (required by /usr/pgsql-17/lib/pg_tde.so)
        linux-vdso.so.1 (0x00007ffd4678f000)
        libcurl.so.4 => /lib64/libcurl.so.4 (0x00007f8c80593000)
        libcrypto.so.3 => /lib64/libcrypto.so.3 (0x00007f8c8007a000)
        libssl.so.3 => /lib64/libssl.so.3 (0x00007f8c7ff94000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f8c7fd8c000)

Complete the setup by creating the extension in database where you plan to use it (or you need to launch the create extension statement on template1 database before creating your own database):

yjaquier=# create extension pg_tde;
CREATE EXTENSION
yjaquier=# \dx
                  List of installed extensions
  Name   |  Version  |   Schema   |         Description
---------+-----------+------------+------------------------------
 pg_tde  | 1.0-beta2 | public     | pg_tde access method
 plpgsql | 1.0       | pg_catalog | PL/pgSQL procedural language
(2 rows)

Then the same trial I have done with the container version is working exactly the same…

Using the ready-to-use pg_tde container

Pull the image with:

PS C:\> podman pull --tls-verify=false perconalab/pg_tde
Trying to pull docker.io/perconalab/pg_tde:latest...
Getting image source signatures
Copying blob sha256:1f4ee32b911dbc7f1bc6366d8e2a2729c2a411fda574c5b0d4ef8633e2324766
Copying blob sha256:7aba9c418e0a57da85792a8eb7178171b61892505e7964c511174349a43350b2
Copying blob sha256:abe67df6d2f493e467b4d0c3b79649239899e5878044ef8fcfa50efb9a7a3aa2
Copying blob sha256:18d1e3e595cbce511f0db857b7b750cf20a20a32404fc6e17ed3fd5cfa7aa8f6
Copying blob sha256:4dc18197e359fa603aa7a6cbe29f12f0e9a0976f7262b1b657c8feee64ddd3c4
Copying blob sha256:be2749fafbe6d84fac8a2d2aca76b431d06c9b7d77cb03afec24fe3c85134bfd
Copying blob sha256:e3dd4f308ad219b2564c579c412b7801e85affa7a6f8ef8598e0f2a61c90675e
Copying blob sha256:af302e5c37e9dc1dbe2eadc8f5059d82a914066b541b0d1a6daa91d0cc55057d
Copying blob sha256:22140276d551ab7931934e7570e59d6270f352adef0a06fe025ec50b1aa3a7ea
Copying blob sha256:175f8f9f9d7758d443325db7873122745c6deb9fa2c5107d7ba54550a12a1eae
Copying blob sha256:62c1901fdf417565a21c3c97ea120978bf3f6f9d6e32858623db2b49f89fe9a9
Copying blob sha256:4026dd09ce086860096c36775c7b1849296c1125548b11d441d04f063808c319
Copying blob sha256:57912421851b20646ddfa68eadefcb3a8771a20e6c16c7d59a509e07a74acc92
Copying blob sha256:09075bc2a281557e12cda5f37dd9b8a779b4bacf07bc021b399a500825526db7
Copying blob sha256:2b2d50a2a35314c2f88f025cd53b731c008e13bc47ca3e7139462562073851aa
Copying blob sha256:60e56e308b3940c9b8603316cee031406713fcbe2a83dfeee18910e862a411da
Copying blob sha256:e54a44a551b2e71f574e316b78894767e1a0afa8e4eb139f52dd75e08c8e03e2
Copying blob sha256:79e4d8c8926c02a7a9e22f0368004daa307d27778dede566c70c3a33fd955df1
Copying blob sha256:d13dbb3668a94fffbef65a2d7aa569716038be609a680a36cbce30dee9b2a5f2
Copying blob sha256:6593dcb87b725d307746cdd8224dd85c8cab785c9faaff06e3b46f2b7fe4d140
Copying blob sha256:c6769a166f36b2c766fd94c278262eb8d4914d83148ac40aac36a9bd9ba8d134
Copying config sha256:40b73f249db5291eefb8a6745f86bff16ca26027660ebdf8b8930135c596d5a5
Writing manifest to image destination
40b73f249db5291eefb8a6745f86bff16ca26027660ebdf8b8930135c596d5a5

Create and run the container with (I map internal port 5432 to 5433):

PS C:\> podman run --detach --name pg-tde --env POSTGRES_PASSWORD=secure_password --publish 5433:5432 perconalab/pg_tde
25d20e66a6f0f9994488b4edc409fe887920a84c52727ea7c98976eb1eba8c7c
PS C:\> podman ps
CONTAINER ID  IMAGE                               COMMAND               CREATED        STATUS        PORTS                   NAMES
25d20e66a6f0  docker.io/perconalab/pg_tde:latest  postgres -c confi...  5 seconds ago  Up 5 seconds  0.0.0.0:5433->5432/tcp  pg-tde

You can connect to it with:

PS C:\> podman exec -it 25d20e66a6f0 /bin/bash
root@25d20e66a6f0:/opt/pg_tde#

Extension pg_tde loaded and active:

postgres=# \dx
                  List of installed extensions
  Name   |  Version  |   Schema   |         Description
---------+-----------+------------+------------------------------
 pg_tde  | 1.0-beta2 | public     | pg_tde access method
 plpgsql | 1.0       | pg_catalog | PL/pgSQL procedural language
(2 rows)

yjaquier=# select * from pg_tde_version();
   pg_tde_version
--------------------
 pg_tde 1.0.0-beta2
(1 row)

Create a database, role, and a tablespace in $PGDATA directory (on standalone installation this folder is /postgres/17/data:

postgres=# create database yjaquier;
CREATE DATABASE
postgres=# create role yjaquier with login superuser createdb createrole inherit noreplication bypassrls connection limit -1 password 'secure_password';
CREATE ROLE
postgres=# create tablespace tbs01 location '/var/lib/postgresql/data';
WARNING:  tablespace location should not be inside the data directory
CREATE TABLESPACE

Create a schema and a non encrypted table with (checkpoint is only to have the file updated on filesystem):

postgres=# \c yjaquier
You are now connected to database "yjaquier" as user "postgres".
yjaquier=# create schema tde;
CREATE SCHEMA
yjaquier=# create table tde.test01(id int, descr varchar(20)) tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test01 values(1, 'One');
INSERT 0 1
yjaquier=# insert into tde.test01 values(2, 'Two');
INSERT 0 1
yjaquier=# checkpoint;
CHECKPOINT
yjaquier=# \dt tde.*
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 tde    | test01 | table | postgres
(1 row)

yjaquier=# SELECT pg_tde_is_encrypted('tde.test01');
 pg_tde_is_encrypted
---------------------
 f
(1 row)
yjaquier=# select pg_relation_filepath('tde.test01');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16474/PG_16_202307071/16472/16476
(1 row)

The data are in clear, as expected:

postgres@25d20e66a6f0:~/data/pg_tblspc/16474/PG_16_202307071/16472$ strings 16476
        Two
        One

Set up a key provider (the keyring file is only for testing purpose as the keys are sored unencrypted):

yjaquier=# select pg_tde_add_key_provider_file('file-keyring','/tmp/pg_tde_test_local_keyring.per');
 pg_tde_add_key_provider_file
------------------------------
                            1
(1 row)

Add a key principal:

yjaquier=# select pg_tde_set_principal_key('yjaquier-db-master-key','file-keyring');
 pg_tde_set_principal_key
--------------------------
 t
(1 row)

yjaquier=# select * from pg_tde_principal_key_info();
   principal_key_name   | key_provider_name | key_provider_id | principal_key_internal_name | principal_key_version |      key_createion_time
------------------------+-------------------+-----------------+-----------------------------+-----------------------+-------------------------------
 yjaquier-db-master-key | file-keyring      |               1 | yjaquier-db-master-key_2    |                     2 | 2025-01-23 14:53:07.304424+01
(1 row)

Then create an encrypted table (using tde_heap_basic) with:

yjaquier=# create table tde.test02(id int, descr varchar(20)) using tde_heap_basic tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test02 values(1, 'One');
INSERT 0 1
yjaquier=# insert into tde.test02 values(2, 'Two');
INSERT 0 1
yjaquier=# checkpoint;
CHECKPOINT
yjaquier=# select pg_tde_is_encrypted('tde.test02');
 pg_tde_is_encrypted
---------------------
 t
(1 row)

yjaquier=# select pg_relation_filepath('tde.test02');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16474/PG_16_202307071/16472/16480
(1 row)

As expected the data file is encrypted and you cannot read data from it:

postgres@25d20e66a6f0:~/data/pg_tblspc/16474/PG_16_202307071/16472$ strings 16480

Pg_tde additional features

Key rotation

The master key can also be rotated (important feature to have for any TDE extension):

[postgres@server01 ~]$ ll /tmp/pg_tde_test_local_keyring.per
-rw------- 1 postgres dba 584 Jan 23 15:35 /tmp/pg_tde_test_local_keyring.per
[postgres@server01 ~]$ psql --dbname=yjaquier
psql (17.2)
Type "help" for help.

yjaquier=# select pg_tde_rotate_principal_key();
 pg_tde_rotate_principal_key
-----------------------------
 t
(1 row)

yjaquier=#
\q
[postgres@server01 ~]$ ll /tmp/pg_tde_test_local_keyring.per
-rw------- 1 postgres dba 876 Jan 24 11:18 /tmp/pg_tde_test_local_keyring.per

WAL encryption

If you go in your WAL directory ($PGDATA/pg_wal) then you can also read in clear the information you entered:

[postgres@server01 pg_wal]$ ll
total 32768
-rw------- 1 postgres dba 16777216 Jan 24 11:22 000000010000000000000002
-rw------- 1 postgres dba 16777216 Jan 23 15:02 000000010000000000000003
drwx------ 2 postgres dba        6 Jan 23 12:37 archive_status
drwx------ 2 postgres dba        6 Jan 23 12:37 summaries
[postgres@server01 pg_wal]$ xxd 000000010000000000000003 | grep -e One -e Two
009edeb0: 094f 6e65 0100 0000 2200 0000 f002 0000  .One....".......
009edf10: 0208 1800 0200 0000 0954 776f 0200 0000  .........Two....
00f20040: 094f 6e65 0100 0000 2200 0000 fe02 0000  .One....".......
00f200a0: 0208 1800 0200 0000 0954 776f 0200 0000  .........Two....

Following the command of the official documentation:

[postgres@server01 ~]$ psql
psql (17.2)
Type "help" for help.

postgres=# alter system set pg_tde.wal_encrypt = on;
ALTER SYSTEM
postgres=#
\q
[postgres@server01 ~]$ cat $PGDATA/postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
shared_preload_libraries = 'pg_tde'
pg_tde.wal_encrypt = 'on'
[postgres@server01  ~]$ pg_restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started

But it does not encrypt automatically the old WAL files:

[postgres@server01 pg_wal]$ xxd 000000010000000000000003 | grep -e One -e Two
009edeb0: 094f 6e65 0100 0000 2200 0000 f002 0000  .One....".......
009edf10: 0208 1800 0200 0000 0954 776f 0200 0000  .........Two....
00f20040: 094f 6e65 0100 0000 2200 0000 fe02 0000  .One....".......
00f200a0: 0208 1800 0200 0000 0954 776f 0200 0000  .........Two....

I had some glitches while using my previous table and so I have decided to restart from scratch and switch the current WAL file:

yjaquier=# drop table tde.test01;
DROP TABLE
yjaquier=# drop table tde.test02;
DROP TABLE
yjaquier=# select pg_walfile_name(pg_current_wal_lsn());
     pg_walfile_name
--------------------------
 000000010000000000000007
(1 row)

yjaquier=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/7006A10
(1 row)

yjaquier=# select pg_walfile_name(pg_current_wal_lsn());
     pg_walfile_name
--------------------------
 000000010000000000000008
(1 row)

Then I recreate and insert three rows in each table:

yjaquier=# create table tde.test01(id int, descr varchar(20)) tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test01 values(1, 'One');
INSERT 0 1
yjaquier=# insert into tde.test01 values(2, 'Two');
INSERT 0 1
yjaquier=# insert into tde.test01 values(3, 'Three');
INSERT 0 1
yjaquier=# create table tde.test02(id int, descr varchar(20)) using tde_heap_basic tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test02 values(4, 'Four');
INSERT 0 1
yjaquier=# insert into tde.test02 values(5, 'Five');
INSERT 0 1
yjaquier=# insert into tde.test02 values(6, 'Six');
INSERT 0 1
yjaquier=# checkpoint
yjaquier-# ;
CHECKPOINT
yjaquier=#
\q
[postgres@server01 pg_wal]$ psql --dbname=yjaquier
psql (17.2)
Type "help" for help.

yjaquier=# select pg_relation_filepath('tde.test01');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16391/PG_17_202406281/16436/24629
(1 row)

yjaquier=# select pg_relation_filepath('tde.test02');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16391/PG_17_202406281/16436/24632
(1 row)

The tables’ datafiles remain respectively for test01 unencrypted and encrypted for test02:

[postgres@server01 pg_wal]$ xxd $PGDATA/pg_tblspc/16391/PG_17_202406281/16436/24629 | grep -i -e one -e two -e three -e four -e five -e six
00001fb0: 0300 0000 0d54 6872 6565 0000 0000 0000  .....Three......
00001fd0: 0200 0200 0208 1800 0200 0000 0954 776f  .............Two
00001ff0: 0100 0200 0208 1800 0100 0000 094f 6e65  .............One
[postgres@server01 pg_wal]$ xxd $PGDATA/pg_tblspc/16391/PG_17_202406281/16436/24632 | grep -i -e one -e two -e three -e four -e five -e six

And if you check the current WAL file, WAL information are encrypted only for the test02 encrypted table (I would have expected for all table or maybe I have not well understood the WAL encryption feature of pg_tde):

[postgres@server01 pg_wal]$ xxd $PGDATA/pg_wal/000000010000000000000008 | grep -i -e one -e two -e three -e four -e five -e six
000035a0: 0020 4070 675f 7469 6d65 7a6f 6e65 5f6e  . @pg_timezone_n
000035c0: 0020 4070 675f 7469 6d65 7a6f 6e65 5f61  . @pg_timezone_a
00003f10: 7274 6974 696f 6e65 645f 7461 626c 6500  rtitioned_table.
00008fa0: 6f6e 6579 0000 000b 0000 0000 0000 0000  oney............
000138d0: 0208 1800 0100 0000 094f 6e65 0100 0000  .........One....
00013940: 0954 776f 0200 0000 2200 0000 1503 0000  .Two....".......
00015240: 0000 000d 5468 7265 6500 0000 0000 0015  ....Three.......
00015260: 0002 0002 0818 0002 0000 0009 5477 6f14  ............Two.
00015280: 0002 0002 0818 0001 0000 0009 4f6e 6503  ............One.

Remove TDE from tables

I have this tde.test02 encrypted table:

yjaquier=# drop table tde.test02;
DROP TABLE
yjaquier=# vacuum;
VACUUM
yjaquier=# create table tde.test02(id int, descr varchar(20)) using tde_heap_basic tablespace tbs01;
CREATE TABLE
yjaquier=# insert into tde.test02 values(1, 'One');
INSERT 0 1
yjaquier=# insert into tde.test02 values(2, 'Two');
INSERT 0 1
yjaquier=# select pg_tde_is_encrypted('tde.test02');
 pg_tde_is_encrypted
---------------------
 t
(1 row)

yjaquier=# select pg_relation_filepath('tde.test02');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16391/PG_17_202406281/16436/24641
(1 row)

Data cannot be stolen:

[postgres@server01 ~]$ xxd $PGDATA/pg_tblspc/16391/PG_17_202406281/16436/24641 | grep -i -e one -e two

I remove TDE from the table with:

yjaquier=# alter table tde.test02 set access method heap;
ALTER TABLE
yjaquier=# select pg_tde_is_encrypted('tde.test02');
 pg_tde_is_encrypted
---------------------
 f
(1 row)

PostgreSQL is recreating a new datafile:

yjaquier=# select pg_relation_filepath('tde.test02');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16391/PG_17_202406281/16436/24644
(1 row)

Initally I had nothing inside:

[postgres@server01 ~]$ xxd $PGDATA/pg_tblspc/16391/PG_17_202406281/16436/24644 | grep -i -e one -e two

But after a checkpoint:

yjaquier=# checkpoint;
CHECKPOINT
yjaquier=# select pg_relation_filepath('tde.test02');
            pg_relation_filepath
---------------------------------------------
 pg_tblspc/16391/PG_17_202406281/16436/24644
(1 row)

We see the data:

[postgres@server01 ~]$ xxd $PGDATA/pg_tblspc/16391/PG_17_202406281/16436/24644 | grep -i -e one -e two
00001fd0: 0200 0200 0208 1800 0200 0000 0954 776f  .............Two
00001ff0: 0100 0200 0208 1800 0100 0000 094f 6e65  .............One

References

About Post Author

Share the knowledge!

Leave a Reply

Your email address will not be published. Required fields are marked *