Techniques and settings that make a PostgreSQL database handle EU personal data in a GDPR-compliant way.
GDPR requires you to protect EU residents’ personal data, limit access, document processing, and erase data on request.PostgreSQL offers built-in features—encryption, row-level security, auditing—that map directly to these obligations.
Query the catalog:SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE column_name ILIKE '%email%' OR column_name ILIKE '%name%' OR column_name ILIKE '%phone%';
Tag findings with the comment
command to keep an audit trail.
Create a symmetric key in a secure vault and reference it in SQL:
CREATE EXTENSION IF NOT EXISTS pgcrypto;
ALTER TABLE customers ADD COLUMN email_enc bytea;
UPDATE customers SET email_enc = pgp_sym_encrypt(email, current_setting('app.symkey'));
ALTER TABLE customers DROP COLUMN email;
Store the key outside the database and pass it through SET app.symkey
per session.
Enable RLS on sensitive tables:
ALTER TABLE customers ENABLE ROW LEVEL SECURITY;
CREATE POLICY customer_is_owner ON customers
USING (id = current_setting('app.current_customer_id')::int);
Now each session sees only its own row, satisfying GDPR’s data-minimization principle.
Create a scheduled job (pg_cron or external scheduler) that anonymizes or deletes expired data:
DELETE FROM orders
WHERE customer_id IN (SELECT id FROM customers WHERE deleted_at IS NOT NULL)
AND order_date < NOW() - INTERVAL '7 years';
For soft deletes, replace identifying columns with hashes using digest()
.
1.Tag personal columns
2. Encrypt with pgcrypto
3. Activate RLS policies
4. Audit access via pgaudit
5. Schedule retention jobs. The next section shows the code end-to-end.
• Always separate keys from data.
• Document policies in the COMMENT
catalog.
• Test RLS with SET ROLE
.
• Log every SELECT on personal tables via pgaudit.log = 'read'
.
.
Yes. TLS protects data in transit; pgcrypto protects it at rest. GDPR requires both layers.
Possible but harder. RLS offers least-privilege access in-database, reducing application code and audit scope.