TEC-DATA86-05 · Lesson 5 · 10 clock hours · lab
Encryption at Rest: TDE Concepts, pgcrypto & Column Encryption
Objective. Learners apply column-level encryption to sensitive fields in PostgreSQL using the pgcrypto extension and describe how Oracle TDE and Azure SQL TDE protect data files at rest.
⚠ Pending professional review
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Encryption at Rest: TDE Concepts, pgcrypto & Column Encryption
Why Encrypt Data at Rest?
Data at rest means data stored on disk—database files, backups, log files, and temporary files. If an attacker steals a physical hard drive or gains raw file access, plaintext data is immediately readable unless encryption protects it. Encryption at rest ensures that even without the database engine, the data remains unreadable. This matters for compliance with frameworks such as HIPAA, PCI-DSS, and SOC 2, and it is tested directly on the Oracle DBA CA, Microsoft DP-300, and DAMA CDMP exams.
Three Approaches Compared
1. Application-Level (Column) Encryption — pgcrypto in PostgreSQL
The `pgcrypto` extension adds cryptographic functions directly inside PostgreSQL. Encryption and decryption happen at the SQL layer before data is written to or read from storage. The database engine itself stores only ciphertext in the column; even a DBA with OS-level access to the data files sees scrambled bytes.
The encryption boundary is at the SQL statement: only a caller who supplies the correct passphrase (or key) can decrypt. This means key management is the application's or DBA's responsibility. If the passphrase is lost, data is unrecoverable.
Core functions:
- `pgp_sym_encrypt(plaintext text, passphrase text)` — returns `bytea` ciphertext using OpenPGP symmetric encryption.
- `pgp_sym_decrypt(ciphertext bytea, passphrase text)` — returns the original plaintext.
Because the output is `bytea`, the encrypted column is defined as `bytea`, not `text`.
2. Oracle Transparent Data Encryption (TDE)
Oracle TDE encrypts data files, redo logs, and undo segments at the storage layer. The word transparent means the database engine decrypts data automatically when an authorized session reads it; applications require no code changes. The encryption boundary is between the Oracle engine and the OS/storage layer.
Oracle TDE supports tablespace-level encryption (the entire tablespace is encrypted) and column-level encryption (individual columns). The master encryption key is stored in an Oracle Wallet or a Hardware Security Module (HSM). If someone steals the data files but not the wallet, they cannot read the data.
3. Azure SQL / SQL Server Transparent Data Encryption (TDE)
Azure SQL Database enables TDE by default on all new databases. It encrypts the entire database data file, log file, and backups using AES-256. The encryption boundary is between the SQL Server engine and the storage subsystem. Two key management modes are available:
- Service-managed TDE: Microsoft manages the Database Encryption Key (DEK) automatically.
- Customer-managed TDE (Bring Your Own Key, BYOK): the customer stores the master key in Azure Key Vault.
Applications connect and query normally; decryption is invisible. This maps to DP-300 exam content on Azure SQL security.
Where the Encryption Boundary Lies
| Approach | Boundary | Who Holds the Key | Transparent to App? |
|---|---|---|---|
| pgcrypto (PostgreSQL) | SQL function call | Application / DBA | No — app must call decrypt function |
| Oracle TDE | Database engine ↔ OS | Oracle Wallet / HSM | Yes |
| Azure SQL TDE | Database engine ↔ Storage | Microsoft or Key Vault | Yes |
Installing pgcrypto and Encrypting a Column
The `pgcrypto` extension ships with the standard PostgreSQL distribution. A superuser installs it once per database:
```sql
CREATE EXTENSION IF NOT EXISTS pgcrypto;
```
An `employees` table with an encrypted SSN column:
```sql
CREATE TABLE employees (
emp_id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
ssn_enc BYTEA
);
```
Inserting a row:
```sql
INSERT INTO employees (name, ssn_enc)
VALUES (
'Jane Smith',
pgp_sym_encrypt('123-45-6789', 'my_strong_passphrase')
);
```
Decrypting for authorized queries:
```sql
SELECT name,
pgp_sym_decrypt(ssn_enc, 'my_strong_passphrase') AS ssn
FROM employees;
```
A direct `SELECT ssn_enc FROM employees` returns binary ciphertext. Inspecting the underlying data file with OS tools (`strings`, `pg_filedump`) reveals no readable SSN.
Passphrase and Key Security
A passphrase hardcoded in application source code defeats the purpose of encryption. Best practices:
- Store passphrases in a secrets manager (HashiCorp Vault, AWS Secrets Manager, Azure Key Vault).
- Rotate keys on a schedule; re-encrypt stored data after rotation.
- Limit which database roles can execute decrypt queries using `GRANT` and row-level security.
- Never log decrypted values; ensure query logging excludes sensitive parameters.
Safety and Operational Cautions
- Key loss = data loss. Always maintain secure, tested backups of encryption keys and wallets in a separate physical location from the encrypted data.
- Performance impact. Column encryption adds CPU overhead on every read/write of encrypted columns. Benchmark before deploying to production.
- Backup encryption. Encrypting the live database does not automatically encrypt logical backups (`pg_dump` output). Pipe dumps through `gpg` or use encrypted backup tools.
- Audit logging. Enable PostgreSQL's `pgaudit` extension or Oracle's Unified Auditing to log who decrypted what and when. Encryption without auditing leaves gaps in your security posture.
- Physical server access. TDE and pgcrypto protect data on stolen disks. They do not protect against a privileged OS user on a running server who can read process memory. Use defense-in-depth: physical security, OS hardening, and network controls alongside encryption.
Key terms10
Encryption at RestThe protection of stored data by converting it to ciphertext so it is unreadable without the correct key, even if the physical storage medium is stolen or accessed outside the normal system.
pgcryptoA PostgreSQL extension that provides cryptographic functions including symmetric and asymmetric encryption, hashing, and random data generation directly within SQL queries.
pgp_sym_encrypt / pgp_sym_decryptpgcrypto functions that encrypt or decrypt a text value using OpenPGP symmetric-key encryption with a caller-supplied passphrase, returning or accepting a bytea value.
Transparent Data Encryption (TDE)A database feature, available in Oracle and SQL Server/Azure SQL, that automatically encrypts data files at the storage layer so applications and users experience no change in how they query data.
Database Encryption Key (DEK)The symmetric key used by SQL Server and Azure SQL TDE to encrypt the actual database pages; the DEK itself is protected by a master key stored in a certificate or Azure Key Vault.
Oracle WalletA secure file-based container used by Oracle Database to store the TDE master encryption key and other credentials; must be open for TDE-encrypted tablespaces or columns to be accessible.
Column-Level EncryptionAn encryption strategy in which only specific columns in a table are encrypted, allowing other columns to be queried normally while protecting the most sensitive fields such as SSNs or payment card numbers.
Encryption BoundaryThe point in a system architecture at which plaintext data is converted to ciphertext; identifying this boundary determines who can access plaintext and what threats are mitigated.
Bring Your Own Key (BYOK)A key management model in cloud services such as Azure SQL where the customer provides and controls the master encryption key through a service like Azure Key Vault rather than relying on provider-managed keys.
byteaA PostgreSQL data type for storing binary strings; the output of pgp_sym_encrypt is bytea because encrypted data is arbitrary binary content, not valid text.
In class: Lab setup: learners install the pgcrypto extension, create an employees table with an encrypted ssn column, and write INSERT and SELECT statements using pgp_sym_encrypt and pgp_sym_decrypt with a passphrase; they confirm that a raw table scan (pg_filedump or strings on the data file) does not reveal plaintext SSN values. · Concept mapping: learners complete a diagram worksheet mapping the pgcrypto approach (application-level) vs. Oracle TDE tablespace encryption vs. Azure SQL service-managed TDE, identifying where the encryption boundary lies in each case and who holds the key. · Written scenario response: given a breach scenario where a hard drive is stolen from a server, learners write a paragraph explaining which encrypted columns or TDE configurations would protect the data and why, referencing their diagram.
Materials: PostgreSQL pgcrypto documentation, Oracle TDE documentation excerpt (docs.oracle.com), Microsoft DP-300 study guide (encryption at rest section), Linux VMs, pgcrypto extension, sample schema SQL
Check: Learner runs instructor-provided SELECT against their table; pgp_sym_decrypt must return the original SSN for authorized query and raw file inspection must show ciphertext only. Pass = both conditions met plus correct written scenario response (instructor rubric: 3 of 3 key points addressed).
Practice check8
Answer each question, then check it. Nothing is recorded; this is your own practice.
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.