HACKER Q&A
📣 shahinghasemi

How to deal with multi-tenant SaaS database structure?


Hello HN, I'm building a SaaS product. Each tenant can have their own database structure/fields while many tables are common between the tenants. For example table A is common for all of them while table B might have different fields upon organization/tenant's need, in other words we have table B for all orgs with different fields.

I'm thinking of shared-codebase and shared-database paradigm for it's simplicity(no synchronization needed). Have you encountered this situation? Is it possible/and how to handle this objective with single database for all tenants? Further, I'm using PostgreSQL database.

Thanks in advance.


  👤 satya71 Accepted Answer ✓
You're heading the right way. A correct design is not simple, but it can be done. You might be able to buy a boilerplate that'll work as a starting point.

Separate database will only work if you'll have a handful of customers, and even then a pain.


👤 codingdave
Possible to do with one database, yes - Add JSON columns to your tables to hold tenant-specific metadata. Deal with it in your app layer.

But I'd re-think whether a single database is truly the right answer. What is driving you to that solution?


👤 ozzythecat
If one tenant misbehaves and takes down your database, how will your other tenants feel?

👤 PaulWaldman
How many tenants and how many clients per tenant will you support?

Also how much data will each tenant consume?


👤 joshxyz
postgresql row level security excels on this right