Encrypted columns
An encrypted column stores each value as AES-256-GCM ciphertext under a key from a secret vault, and decrypts it only on the way out, for a role allowed to use that vault. Use it for identity numbers, card numbers, health data and other fields that must not be readable from the files, the backups, the replicas or by roles that can read the rest of the table.
Encrypted columns are part of the enterprise server (rasterizeddb-enterprise). They work on ordinary tables and on ledger tables.
Declare an encrypted column
First make a vault and an AES key in it, then declare the column with ENCRYPTED WITH VAULT <vault> KEY '<key>' when the table is created:
CREATE VAULT hr KEY PROVIDER 'mlkem:/etc/rasterizeddb/hr.mlkem;create';
VAULT hr CREATE KEY 'pii' TYPE 'aes256-gcm';
CREATE TABLE patients (
id BIGINT PRIMARY KEY,
name TEXT,
ssn VARCHAR(11) ENCRYPTED WITH VAULT hr KEY 'pii' NOT NULL
);- The column's type is
TEXT,VARCHAR(n)orCHARACTER VARYING(n). AVARCHAR(n)limit is checked on the plaintext (SQLSTATE 22001 when it is too long). - The key is an
aes256-gcmkey of the vault, or anhsm-aes256-gcmkey on apkcs11:vault (sealing then runs on the HSM). See Keys used in place. - After
KEY '<key>'the column takesNOT NULLorNULLonly. A primary key,UNIQUE,CHECK,DEFAULTorREFERENCESon it, or a table constraint naming it, is refused: each would compare or compute on ciphertext. Write the type beforeENCRYPTED, and nothing else. - The role creating the table needs
SELECTon the vault. - Each column gets its own random data key, wrapped by the vault key.
SHOW LEDGERlists every encrypted column:
encrypted column patients.ssn: vault hr key 'pii', varchar(11)A column is declared encrypted only when its table is created. Refused declarations:
ALTER TABLE … ADD COLUMN … ENCRYPTED …;CREATE TEMP TABLE,CREATE UNLOGGED TABLE;CREATE TABLE IF NOT EXISTS(create the table once, without it);- a schema-qualified table name (
CREATE TABLE public.patients …); CREATE TABLE … AS.
On a ledger table the declaration is the same:
CREATE LEDGER TABLE cards (id BIGINT NOT NULL, pan TEXT ENCRYPTED WITH VAULT hr KEY 'pii')
NO DELETE UNTIL 7 YEARS AFTER INSERT;The chain records the ciphertext, so VERIFY LEDGER checks the stored values without decrypting them.
Write and read
An encrypted column is written and read whole:
INSERT INTO patients (id, name, ssn) VALUES (1, 'Ann', '123-45-6789'), (2, 'Bob', '987-65-4321');
INSERT INTO patients VALUES (3, 'Cy', '555-12-3456'); -- by position
UPDATE patients SET ssn = '555-55-5555' WHERE id = 2;
SELECT id, name, ssn FROM patients WHERE id = 1;
SELECT * FROM patients ORDER BY id;
DELETE FROM patients WHERE id = 3 RETURNING ssn;What may name an encrypted column:
- a whole item of the select list or of
RETURNING:ssn,p.ssn,ssn AS s, or*; - a value of
INSERT … VALUES, by column list or by position: a literal,NULL,DEFAULT(which isNULL) or a bound parameter ($1), one row or many; UPDATE … SET ssn = <literal | NULL | $n>.
Prefer bound parameters for the values: a literal is part of the statement text the client sends. The server replaces it with its ciphertext before the statement runs, so only ciphertext reaches the table, the write-ahead log and the replication stream. NULL stays NULL.
What is refused
Everything else that names an encrypted column is refused before it runs (SQLSTATE 0A000), because an answer computed over ciphertext would be wrong without an error:
SELECT id FROM patients WHERE ssn = '123-45-6789';ERROR: column "patients.ssn" is encrypted: … An encrypted column can only be read whole (in a select list or RETURNING) or written whole (a literal in INSERT … VALUES or UPDATE … SET); it cannot be filtered, sorted, grouped, joined on, compared, indexed or computed withThat covers:
WHERE(includingIS NULL,LIKE,INand a bound parameter),JOIN … ON,HAVING;ORDER BYandGROUP BYby name, by alias or by position (ORDER BY 2);DISTINCT, aggregates (count(DISTINCT ssn),max(ssn)), functions, casts, concatenation;UNION,INTERSECT,EXCEPT, subqueries and CTEs that name it,SELECT … INTO;INSERT … SELECT,INSERT … ON CONFLICT,CREATE TABLE … AS SELECT,COPY;- a value that is an expression (
SET ssn = upper(ssn),VALUES (…, upper('x'))); CREATE INDEXon it, renaming the table or any of its columns, and dropping the column.
Filter, sort and join on the table's other columns as usual: SELECT ssn FROM patients WHERE id = 1 is allowed, SELECT id FROM patients WHERE ssn = … is not.
Who sees plaintext
Values are decrypted as the result leaves the server:
| Reader | Sees |
|---|---|
A role with SELECT on the column's vault | The plaintext. |
| A superuser | The plaintext. |
| An embedded application calling the engine in-process | The plaintext. |
Any other role, even with SELECT on the table | The stored ciphertext, rdbenc1:<column id>:<base64>. |
Writing needs the same grant, since sealing a value uses the vault's key. Without it:
ERROR: permission denied for vault "hr" (SELECT): its key encrypts this columnGRANT SELECT, INSERT ON patients TO app;
GRANT SELECT ON hr TO app; -- app may now read and write ssn in plaintextThe ciphertext belongs to its column, not to its place: a value copied as text into another table still decrypts for a role holding that column's vault, and only for such a role. Studio shows a value it cannot decrypt as "🔒 encrypted (no access to its vault)".
Keys and rotation
- Rotating the vault's key-encryption key (
VAULT hr ROTATE KEYorROTATE KEY TO '<provider>') rewraps the vault key; the columns' data keys and the stored values are unchanged and stay readable. See Rotating the key-encryption key. - The vault key itself (
pii) cannot be disabled, deleted or purged: the secret statements refuse names of keys. Dropping the vault leaves the column without a key. - A column's data key is kept, wrapped, in the
encrypted_columnsfile beside the database's chain (see Files beside the chain). Without that file, the vault's keyring and the provider's key, the values cannot be decrypted. If the file cannot be read, statements on that database fail, rather than run as if the columns were plain. - Dropping the table forgets its columns and their keys. A table created again with the same name is a new table.
What the rest of the system sees
- Files, write-ahead log, backups. Ciphertext only.
- Replicas. A replica stores the same ciphertext but not the column keys, so it returns ciphertext. Read encrypted columns on the primary.
- A server without the enterprise features refuses to open a data directory that has held an encrypted column, rather than serve ciphertext as values.
- Performance. A statement that names a table with an encrypted column always takes the server's guarded text path: no prepared-statement or learned parse reuse, and none of the native protocol's direct paths. Expect such statements to cost more than the same statement on a plain table.
Limits
- No search on an encrypted column: no equality lookup, no index, no sort. Keep a separate column (for example a keyed hash computed by your application) if you need to find rows by it.
- Only text types: store numbers and dates as text.
- A column is made encrypted only at
CREATE TABLE; an existing column cannot be encrypted, and an encrypted one cannot be decrypted in place. Copy the data through your application into a new table. - A table with encrypted columns cannot be renamed, and neither can its columns.
COPYinto or out of such a table is refused; useINSERT.- There is no statement that re-encrypts a column under a different vault key.
- Replicas return ciphertext.