Encryption — Complete Guide
Encryption — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 73 of 100
Encryption
SQL ✓ → Advanced
Advanced · 2 — Production · ~10 min · PostgreSQL — Security & Cloud
What is this?
Encryption protects data at rest (disk/TDE provider features) and in transit (SSL). Application-level pgcrypto encrypts sensitive columns like PAN tokens.
Why should you care?
PostgresVerse stores UPI tokens — pgcrypto column encryption plus disk encryption meets PCI-style expectations.
See it live — copy this example
Run in pgAdmin or psql.
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE payment_tokens (
token_id bigserial PRIMARY KEY,
customer_id bigint,
pan_enc bytea NOT NULL
);
INSERT INTO payment_tokens (customer_id, pan_enc)
VALUES (1, pgp_sym_encrypt('4111111111111111', 'vault-key-from-kms'));
SELECT pgp_sym_decrypt(pan_enc, 'vault-key-from-kms') FROM payment_tokens WHERE token_id = 1;
What happened?
- pgp_sym_encrypt stores ciphertext in bytea.
- Key must come from vault not hardcoded in prod.
- Decrypt only in controlled backend function.
Practice next
- CREATE EXTENSION pgcrypto.
- Insert encrypted PAN sample with dev key.
- Decrypt in psql lab only.
- Use pgp_pub_encrypt asymmetric pattern for batch jobs.
- Store hash for lookup, encrypt for display-only fields.
Remember
pgcrypto for column-level symmetric encryption. Disk encryption is infra provider responsibility. Keys in KMS/HSM not database.
PostgresVerse token vault
Payment service decrypts only in HSM-backed worker; DB stores bytea ciphertext.
Outcome: DBA snapshot leak exposes useless bytes without key.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!