Practical techniques to store, process, and delete personal data in PostgreSQL according to EU GDPR.
GDPR requires you to minimize stored personal data, secure it in transit and at rest, audit access, and delete it on request. PostgreSQL supports all four pillars through native features and extensions.
Use pgcrypto for column-level encryption or enable filesystem/volume encryption.pgcrypto keeps keys outside the database, satisfying separation-of-duties guidelines.
Store encrypted emails in Customers
:
ALTER TABLE Customers
ADD COLUMN email_cipher bytea;
UPDATE Customers
SET email_cipher = pgp_sym_encrypt(email, :key);
ALTER TABLE Customers DROP COLUMN email;
Combine Row Level Security (RLS) with SECURITY DEFINER views.Only approved roles run decryption functions, protecting plaintext from curious users.
Create a stored procedure that deletes or anonymizes customer rows and cascades through related tables (Orders
, OrderItems
). Log each invocation for auditability.
Enable log_statement = 'mod'
and install pgaudit
to capture SELECTs on sensitive tables.Ship logs to long-term, immutable storage.
• Keep encryption keys outside the DB.
• Hash or mask personal data before using it in analytics.
• Remove unused PII columns quickly.
• Test erasure procedure in staging monthly.
1. Forgetting to encrypt backups.
2. Creating ordinary indexes on plaintext PII columns.
3. Using application-only deletes without DB-level cascade.
.
Encryption adds CPU overhead, but the impact is minimal for point lookups. Benchmark with your workload.
No. Masking hides data from users; encryption protects it at storage level. Use both for strong compliance.
GDPR does not mandate a period; retain logs only as long as they serve a lawful purpose, typically 6–24 months.