Preamble
A version of PostgreSQL called PostgreSQL TDE provides on-disk encryption for PostgreSQL. Currently, this software is maintained for use by the general public. We’ve recently been asked how to combine PostgreSQL TDE with key management solutions that are already out there. We have chosen to lay out the fundamental ideas and assist people in putting key management strategies for PostgreSQL to secure their infrastructures into practice in order to assist you manage encryption keys with PostgreSQL TDE.
PostgreSQL TDE: How encryption keys are handled
Running PostgreSQL TDE is easy. However, it necessitates a fundamental comprehension of how keys are typically handled.
Deploying a PostgreSQL instance
Behind the scenes, initdb runs when a new PostgreSQL instance is built. When the service is started for the first time, most systems take care of that for you. Initdb can do things like deploy a config file, create the initial content of the data directory, and more.
The most important thing to remember is that TDE (Transparent Data Encryption) needs the data files on a disk to be encrypted. The obvious issue that follows is: How do we locate the key? To handle keys effectively, we need to be a little inventive because it’s not a good idea to store the key directly in the data directory.
When creating PostgreSQL TDE, we chose a straightforward plugin design. Key management practices can vary widely. There are many different keystores, and each company has different needs based on use cases, philosophies, rules, and maybe even the law.
PostgreSQL TDE uses a plugin, which is an executable we must specify, to get the key:
[tde@cybertec]$ initdb --help
initdb initializes a PostgreSQL database cluster.
Usage:
initdb [OPTION]... [DATADIR]
Options:
... -k, --data-checksums use data page checksums -K, --encryption-key-command command that returns encryption key
The -K option requires you to point to a script that returns the key as a string. Note that this key is never visible to anybody but the database server. It does not show up in a process table. It does not show up anywhere. The beauty of this design is that any plugin can be created for any key manager. You can even run the command line and have the administrator enter the key to conduct tests. Although hardly anyone in the real world uses that strategy, it is still an option.
As stated: Provide a script or an executable that echoes the key to stdout. PostgreSQL will read it and use it to encrypt your data files. Having this script gives you complete freedom to work with any type of key management.
Here is an example:
[tde@cybertec]$ ./initdb -D /path/db -K /path/key_manager.sh
User “hs” will be the owner of the database system’s files.
The server process must also belong to this user.
The database cluster’s initialization will include locales
COLLATE: en_US. en_US. UTF-8 CTYPE: en_US. en_US. UTF-8 MESSAGES: en_US.UTF-8 MONETARY: de_DE UTF-8 NUMERIC: de_DE UTF-8 TIME: de_DE. UTF-8
Accordingly, the default database encoding has been set to “UTF-8.”
English will be selected as the default text search configuration.
There are no data page checksums.
Data encryption is enabled.
creating the directory /path/db, ok creating subdirectories, ok selecting the dynamic shared memory implementation, ok posix: selecting default max_connections: 100 selecting default shared_buffers ... 128MB selecting default time zone ... Europe/Vienna creating configuration files, ok running the bootstrap script, ok performing post-bootstrap initialization, ok syncing data to disk, ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
[tde@cybertec]$ ./pg_ctl -D /path/db -l logfile start
The-K option is really the path to success here. In the simplest of all cases, the script simply returns a constant:
[tde@cybertec]$ cat /path/key_manager.sh #!/bin/sh echo 4e5358ab309bcdea23450934546298ab
However, as was already mentioned, this is merely a showcase. Do not keep the key on the same server; rather, integrate it with a reliable key repository and manage the key remotely. Otherwise, TDE would be pointless and simply a waste of resources.
It’s crucial to keep in mind that the key must be 32 characters long and written in hexadecimal. Otherwise, an errorinitdb will occur.
initdb: fatal: encryption key is too short, should be a 32 character hex key
There is bundled PG Keytool software for using secrets that people can memorize that accepts a passphrase as input and uses a key derivation function to create a properly formatted output key. This adds another layer of protection against dictionary attacks by using a key derivation function that takes a lot of time to run.
We can start PostgreSQL normally once the instance has been deployed.
Starting PostgreSQL
What initdb has done for us is to adjust the relevant values in postgresql.conf:
# - Encryption - encryption_key_command = '/path/key_manager.sh'
The same program to retrieve the key is run every time the server boots up. Keep in mind that to decrypt those data files, we need this key. If the program does indeed return the correct key, the server has already taken precautions to ensure that nothing can go wrong. The server cannot be started if the key is not available. No back door, emergency key, or similar features exist. Your data is lost if you cannot provide the right key. The entire concept behind TDE is that. Backdoors are not what we want.
Ensuring encryption is enabled
How can I check to see if my PostgreSQL instance is encrypted, a common question? The way to do that is to control pg_controldata:
[tde@cybertec]$ pg_controldata /path/db/ ... Database system identifier: 7036323720088499175 Database cluster state: shut down pg_control last modified: Di 30 Nov 2021 12:29:41 CET ... Mock authentication nonce: f12fd3308a81f946ffbb36a0a3bd4d41c87bbb9ba4d612b809a001a2202cdc6a Data encryption: on Data encryption fingerprint: D2D2CED8FE9F3980599289B1F468A9FB
You can find the encryption information at the bottom of the listing. Make sure that you’re using the right version of pg_controldata. Otherwise, you won’t be able to find that information.
Popular keystores recommended for PostgreSQL TDE encryption keys
One of the main questions is: Which key store can one use for PostgreSQL? The answer is: any of them. Here are some that are quite popular and widely used by many companies. Be aware that this is not a comprehensive list.
- nitrokey.com
- YubiHSM
- HashiCorp Vault
In a typical production system, the database master key is made at random, encrypted with a key from a key management system, and then stored in the PostgreSQL data directory. This means that the whole database doesn’t have to be re-encrypted, and keys can be changed or even the key management system can be replaced.
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Transforming Healthcare and E-commerce Efficiency: How Enteros Leverages Generative AI to Optimize SaaS Database Performance and Drive Digital Innovation
- 10 November 2025
- Database Performance Management
Introduction In an era defined by data-driven transformation, both the healthcare and e-commerce sectors stand as two of the most dynamic and fast-evolving industries. While their missions differ — one saves lives and the other shapes consumer experiences — both share a common foundation: data.Every patient interaction, online purchase, diagnostic scan, or personalized recommendation depends … Continue reading “Transforming Healthcare and E-commerce Efficiency: How Enteros Leverages Generative AI to Optimize SaaS Database Performance and Drive Digital Innovation”
Driving RevOps Excellence in the Technology Sector: How Enteros Combines AIOps Intelligence and Database Performance Management for Superior Operational Efficiency
Introduction The technology sector thrives on innovation, speed, and precision. As organizations accelerate digital transformation, the pressure to maintain database performance, system reliability, and cost efficiency intensifies. With expanding workloads, hybrid cloud infrastructures, and distributed databases, achieving seamless performance management across platforms becomes increasingly complex. This complexity directly impacts Revenue Operations (RevOps) — the strategic … Continue reading “Driving RevOps Excellence in the Technology Sector: How Enteros Combines AIOps Intelligence and Database Performance Management for Superior Operational Efficiency”
Why AI Projects Fail Before They Start — Data Quality First
Insight for CIOs, FinOps and IT Leaders in 2025 Introduction AI is everywhere in boardroom conversations: promises of automation, predictive insights, and competitive advantage. Yet behind the hype lies a sobering reality — most AI projects stall before they deliver measurable value. The paradox is striking: the algorithms are powerful, but the data feeding them … Continue reading “Why AI Projects Fail Before They Start — Data Quality First”
Revolutionizing the BFSI Sector: How Enteros Harnesses Generative AI and AIOps for Next-Generation Performance Management
- 9 November 2025
- Database Performance Management
Introduction In the fast-evolving Banking, Financial Services, and Insurance (BFSI) sector, digital transformation is not just a competitive advantage—it’s an operational necessity. Every second of downtime, lagging transaction, or database bottleneck can translate into millions in lost revenue, compliance risks, and diminished customer trust. The BFSI industry depends on robust, scalable, and intelligent systems that … Continue reading “Revolutionizing the BFSI Sector: How Enteros Harnesses Generative AI and AIOps for Next-Generation Performance Management”