3 Hours

18. Vault, Encryption & Secrets Management

Security

Encrypting Sensitive Data at the Database Level

Supabase Vault is a Postgres extension (supabase_vault) built on pgsodium that enables column-level encryption inside the database. API keys, PII (personally identifiable information), health records, and financial data can be stored encrypted and only decrypted by functions with the correct access — not exposed through the REST API.

Encryption is essential for protecting sensitive data. Even with RLS, there are scenarios where encryption is needed — data at rest, protection against database administrators, or compliance requirements. Supabase Vault provides a secure way to store secrets and encrypted data. Vault uses envelope encryption — data is encrypted with a data encryption key (DEK), and the DEK is encrypted with a key encryption key (KEK). This means even if someone gets the database file, they can't decrypt the data without the KEK. Vault secrets are stored encrypted and can only be decrypted by privileged functions. This gives you a secure way to store API keys, encryption keys, and other sensitive data.

Vault: Storing Encrypted Secrets

Vault provides a simple API for storing and retrieving secrets. Use vault.create_secret() to store a secret. The secret is encrypted before storage. Use vault.decrypted_secrets to retrieve the decrypted value — only accessible to postgres role or SECURITY DEFINER functions. Store API keys, encryption keys, and other sensitive data in Vault. This is more secure than storing them in plain text or even in environment variables. Vault secrets are encrypted at rest. They're also not exposed through the REST API. Only privileged functions can decrypt them. This is the most secure way to manage secrets in Supabase.

SQL
-- Store a secret (e.g., a third-party API key per user)
SELECT vault.create_secret(
  'sk_live_stripe_key_for_user_123',
  'stripe_key_user_123',
  'User 123 Stripe secret key'
);

-- Retrieve a secret (only accessible to postgres role / SECURITY DEFINER functions)
SELECT decrypted_secret FROM vault.decrypted_secrets
WHERE name = 'stripe_key_user_123';

Envelope Encryption

Vault uses envelope encryption: data is encrypted with a data encryption key (DEK), and the DEK itself is encrypted with a key encryption key (KEK) stored in pgsodium's key manager. This means even if someone gets the database file, they cannot decrypt the data without the KEK.

Column-Level Encryption with pgsodium

For column-level encryption of sensitive data like PII or health records, use pgsodium. The pgsodium extension provides authenticated encryption with associated data (AEAD). This both encrypts the data and prevents tampering. Create an encryption key with pgsodium.create_key(). Store the key ID in a table. Encrypt data using pgsodium.crypto_aead_det_encrypt(). This takes the plaintext, additional authenticated data (like the table name), and the key ID. The encrypted data is stored as text. To decrypt, use pgsodium.crypto_aead_det_decrypt(). Always use SECURITY DEFINER functions for decryption. This ensures the key is never exposed to regular users. This is the most secure way to handle sensitive data in Supabase.

SQL
-- Enable pgsodium for column-level encryption
CREATE EXTENSION IF NOT EXISTS pgsodium;

-- Create an encryption key
SELECT pgsodium.create_key(
  name := 'patient_data_key',
  comment := 'Key for encrypting patient health records'
);

-- Table with encrypted sensitive field
CREATE TABLE patient_records (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  patient_id UUID REFERENCES auth.users(id),
  encrypted_diagnosis TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

Security Best Practices

Follow these security best practices for encryption. Never store raw API keys in regular columns — use Vault or Edge Function secrets. Use SECURITY DEFINER functions as the only way to decrypt data — the decryption key never leaves the database. Audit access to vault.secrets — log every decryption in an audit table. Rotate encryption keys periodically — pgsodium supports key rotation without re-encrypting all data. Use RLS on encrypted tables — even if data is encrypted, unauthorized users shouldn't see even the ciphertext. These practices ensure your sensitive data is protected at all times. Encryption is your last line of defense — use it alongside RLS and other security controls.

  • Never store raw API keys in regular columns — use Vault or Edge Function secrets.
  • Use SECURITY DEFINER functions as the only way to decrypt data — the decryption key never leaves the database.
  • Audit access to vault.secrets — log every decryption in an audit table.
  • Rotate encryption keys periodically — pgsodium supports key rotation without re-encrypting all data.
  • Use RLS on encrypted tables — even if data is encrypted, unauthorized users shouldn't see even the ciphertext.

Knowledge Check

Ready to test your understanding of 18. Vault, Encryption & Secrets Management?

Back