<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd" -->

---
title: Multi-tenant BYOK encryption in PostgreSQL with pgcrypto
description: A practical guide to implementing per-tenant BYOK (Bring Your Own Key) column encryption in a multi-tenant PostgreSQL database using pgcrypto. The approach...
canonical: https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Multi-tenant BYOK encryption in PostgreSQL with pgcrypto | daily.dev
og:description: A practical guide to implementing per-tenant BYOK (Bring Your Own Key) column encryption in a multi-tenant PostgreSQL database using pgcrypto. The approach...
og:url: https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd
og:image: https://api.daily.dev/og/posts/yMn2in4hd.png
og:image:alt: Multi-tenant BYOK encryption in PostgreSQL with pgcrypto
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi-tenant BYOK encryption in PostgreSQL with pgcrypto

**[Planet PostgreSQL](https://daily.dev/sources/planet-postgresql)** · 7 min read · 0 upvotes · 0 comments

## Summary

A practical guide to implementing per-tenant BYOK (Bring Your Own Key) column encryption in a multi-tenant PostgreSQL database using pgcrypto. The approach uses envelope encryption: AWS KMS holds a Key Encryption Key (KEK) per organization, while the application briefly holds a Data Encryption Key (DEK) in memory and passes it directly into Postgres queries. The post covers schema design, data seeding with pgp_sym_encrypt, single-org and cross-org read patterns, error behavior on wrong-key access, and production considerations such as binding keys as parameters, tracking encryption state per row, handling key rotation, and giving non-BYOK tenants a platform-managed DEK.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://postgr.es/p/9s2>

## Questions this post answers

### How do I implement per-tenant bring-your-own-key encryption in a shared multi-tenant PostgreSQL table?

Use pgcrypto's pgp_sym_encrypt and pgp_sym_decrypt with envelope encryption: store a customer-managed Key Encryption Key (KEK) reference like a KMS ARN on each organization, have the application unwrap a per-org Data Encryption Key (DEK) via KMS Decrypt at request time, and pass that plaintext DEK as a bind parameter into the query. Postgres never stores the raw DEK, and non-BYOK tenants can use a platform-managed key so the column stays ciphertext for everyone.

_Teams designing tenant-level encryption schemes can dig deeper into pgcrypto patterns on daily.dev._

### What happens in PostgreSQL if you call pgp_sym_decrypt with the wrong key?

pgp_sym_decrypt throws a hard error, 'Wrong key or corrupt data', rather than silently returning garbage or the wrong plaintext. This happens because the PGP format includes an integrity check, so a mismatched key is rejected outright instead of producing corrupted output that could be mistaken for valid data.

_Developers verifying encryption safety nets can compare notes on pgcrypto behavior via daily.dev._

### How should I query encrypted data across multiple tenants that use different encryption keys in the same table?

Use a CTE that maps each organization to its unwrapped DEK, then LEFT JOIN it against the rows so a CASE statement picks pgp_sym_decrypt for BYOK organizations and convert_from for plaintext non-BYOK rows. Organizations absent from the CTE naturally fall into the non-BYOK branch since no DEK is provided for them.

_Anyone building admin or analytics views over mixed-encryption tenant data can find similar SQL patterns on daily.dev._

## Similar posts on daily.dev

- [Percona Brings Transparent Data Encryption to Postgres](https://daily.dev/posts/percona-brings-transparent-data-encryption-to-postgres-uogimj7db) · The New Stack · 1 upvotes · 0 comments
- [BYOK for existing Elastic Cloud deployments is now GA](https://daily.dev/posts/byok-for-existing-elastic-cloud-deployments-is-now-ga-suahfmztk) · elastic · 0 upvotes · 0 comments
- [What is Bring Your Own Encryption \(BYOE\)?](https://daily.dev/posts/what-is-bring-your-own-encryption-byoe--yh4fqrkpx) · Security Boulevard · 0 upvotes · 0 comments
- [Take control of your encryption keys with Bring Your Own Key on AWS](https://daily.dev/posts/take-control-of-your-encryption-keys-with-bring-your-own-key-on-aws-vxrsnt30q) · Dynatrace · 0 upvotes · 0 comments
- [Take Control: Customer-Managed Keys for Lakebase Postgres](https://daily.dev/posts/take-control-customer-managed-keys-for-lakebase-postgres-urvwpz1do) · databricks · 0 upvotes · 0 comments

---

Tags: [#postgresql](https://daily.dev/tags/postgresql), [#encryption](https://daily.dev/tags/encryption), [#multi-tenancy](https://daily.dev/tags/multi-tenancy)

[View this post on daily.dev](https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Multi-tenant BYOK encryption in PostgreSQL with pgcrypto","url":"https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd"},"datePublished":"2026-08-11T13:05:29.934Z","dateModified":"2026-09-14T09:19:50.961Z","description":"A practical guide to implementing per-tenant BYOK (Bring Your Own Key) column encryption in a multi-tenant PostgreSQL database using pgcrypto. The approach...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c2a27f315ffedc26be4ea45453031708?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c2a27f315ffedc26be4ea45453031708?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet PostgreSQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet PostgreSQL","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/planet-postgresql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"postgresql,encryption,multi-tenancy","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet PostgreSQL","item":"https://daily.dev/sources/planet-postgresql"},{"@type":"ListItem","position":3,"name":"Multi-tenant BYOK encryption in PostgreSQL with pgcrypto"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/multi-tenant-byok-encryption-in-postgresql-with-pgcrypto-ymn2in4hd#faq","mainEntity":[{"@type":"Question","name":"How do I implement per-tenant bring-your-own-key encryption in a shared multi-tenant PostgreSQL table?","acceptedAnswer":{"@type":"Answer","text":"Use pgcrypto's pgp_sym_encrypt and pgp_sym_decrypt with envelope encryption: store a customer-managed Key Encryption Key (KEK) reference like a KMS ARN on each organization, have the application unwrap a per-org Data Encryption Key (DEK) via KMS Decrypt at request time, and pass that plaintext DEK as a bind parameter into the query. Postgres never stores the raw DEK, and non-BYOK tenants can use a platform-managed key so the column stays ciphertext for everyone. Teams designing tenant-level encryption schemes can dig deeper into pgcrypto patterns on daily.dev."}},{"@type":"Question","name":"What happens in PostgreSQL if you call pgp_sym_decrypt with the wrong key?","acceptedAnswer":{"@type":"Answer","text":"pgp_sym_decrypt throws a hard error, 'Wrong key or corrupt data', rather than silently returning garbage or the wrong plaintext. This happens because the PGP format includes an integrity check, so a mismatched key is rejected outright instead of producing corrupted output that could be mistaken for valid data. Developers verifying encryption safety nets can compare notes on pgcrypto behavior via daily.dev."}},{"@type":"Question","name":"How should I query encrypted data across multiple tenants that use different encryption keys in the same table?","acceptedAnswer":{"@type":"Answer","text":"Use a CTE that maps each organization to its unwrapped DEK, then LEFT JOIN it against the rows so a CASE statement picks pgp_sym_decrypt for BYOK organizations and convert_from for plaintext non-BYOK rows. Organizations absent from the CTE naturally fall into the non-BYOK branch since no DEK is provided for them. Anyone building admin or analytics views over mixed-encryption tenant data can find similar SQL patterns on daily.dev."}}]}
```

