NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / ENTERPRISE / ENCRYPTED COLUMNS

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:

sql
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) or CHARACTER VARYING(n). A VARCHAR(n) limit is checked on the plaintext (SQLSTATE 22001 when it is too long).
  • The key is an aes256-gcm key of the vault, or an hsm-aes256-gcm key on a pkcs11: vault (sealing then runs on the HSM). See Keys used in place.
  • After KEY '<key>' the column takes NOT NULL or NULL only. A primary key, UNIQUE, CHECK, DEFAULT or REFERENCES on it, or a table constraint naming it, is refused: each would compare or compute on ciphertext. Write the type before ENCRYPTED, and nothing else.
  • The role creating the table needs SELECT on the vault.
  • Each column gets its own random data key, wrapped by the vault key. SHOW LEDGER lists every encrypted column:
text
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:

sql
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:

sql
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 is NULL) 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:

sql
SELECT id FROM patients WHERE ssn = '123-45-6789';
text
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 with

That covers:

  • WHERE (including IS NULL, LIKE, IN and a bound parameter), JOIN … ON, HAVING;
  • ORDER BY and GROUP BY by 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 INDEX on 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:

ReaderSees
A role with SELECT on the column's vaultThe plaintext.
A superuserThe plaintext.
An embedded application calling the engine in-processThe plaintext.
Any other role, even with SELECT on the tableThe stored ciphertext, rdbenc1:<column id>:<base64>.

Writing needs the same grant, since sealing a value uses the vault's key. Without it:

text
ERROR:  permission denied for vault "hr" (SELECT): its key encrypts this column
sql
GRANT SELECT, INSERT ON patients TO app;
GRANT SELECT ON hr TO app;   -- app may now read and write ssn in plaintext

The 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 KEY or ROTATE 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_columns file 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.
  • COPY into or out of such a table is refused; use INSERT.
  • There is no statement that re-encrypts a column under a different vault key.
  • Replicas return ciphertext.
✓ page rendered in 140 µs axum + tera + htmx · no other javascript