# Multi-tenancy

Multi-tenancy is an architecture in which one installation of an application serves several customers, the tenants, while keeping each tenant's data and settings separate from the others. The separation must be enforced where the data lives, not in the interface, and Meadowstack 2.1 leaves that wall to your backend while its rules keep other clients' records out of every screen.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/glossary/multi-tenancy/

*Three common ways to separate tenants in PostgreSQL, and where the separation is enforced in each.*

| Design | How tenants are separated | Where a mistake leaks |
| --- | --- | --- |
| Shared tables | Every row carries the tenant's id; row-level security policies compare it with the signed-in person's tenant | A table without a policy, or an api role that bypasses row security |
| Schema per tenant | Each tenant has its own schema with the same tables; the connection picks the schema | Code that sets the wrong schema for a request |
| Database per tenant | Each tenant has its own database and credentials | Credentials or a backup that reaches the wrong tenant |

## Why it matters in a business app

The first serious leak in a business app is rarely a hacker; it is one client seeing another client's invoice because a query forgot a filter. A studio that builds one app for many franchisees, or a product sold to several companies, lives with that risk on every new screen.

Shared tables with [row-level security](https://meadowstack.com/glossary/row-level-security/) are the usual start, because one schema is easier to migrate. PostgreSQL's warning applies twice here: superusers, roles with `BYPASSRLS` and, unless the table forces row security, its owner all bypass the policies, and then every tenant sees every row.

## How Meadowstack handles it

In 2.1, honestly:

- **No tenant model ships.** The starter is one company; the Supabase example has no tenant column. The demo's several companies are separate simulated datasets, not a multi-tenant backend.
- **The rule that comes closest** is in the root AGENTS.md: an external role (client, customer, guest) never sees internal costs, margins or other clients' records, in widgets, feeds, the bell, search and assistant answers as on the pages, and screens ask with the record, `can("site.read", site)`.
- **What you add:** a tenant id on every row, set by the server from the session, policies that compare it, an api role bound by them, and a test that signs in as one tenant and asks for another's records.

Meadowstack 3.0, coming as an update to every licence, adds the rule "check ownership on every record action: the record belongs to the user's organisation" to the rules coding tools follow.

## Common mistakes

- **The tenant id from the browser.** It comes from the session on the server, never from a field the page sends.
- **Filtering in the api only.** One forgotten `where` is a leak; a policy in the database holds when the api forgets.
- **Shared caches and search indexes.** A cache key or an index without the tenant serves one tenant's answer to another.
- **Exports and the assistant.** They read through a different path; give them the same rule.

## Questions

### Is a multi-tenant app less secure than one installation per client?

Not by design, but a mistake reaches more people. Shared tables need the separation enforced in the database and tested; separate databases trade that risk for more to run and update.

### Can I build a multi-tenant SaaS with Meadowstack?

Yes, with your own backend: the interface, the permission rules and the screens are the same, and the tenant wall lives in your api and database.

### Where should the tenant id come from?

From the signed-in person's session or token, read on the server. The browser may show it, never decide it.

## Sources

- [PostgreSQL documentation, Row Security Policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html), checked 2 December 2026

## Related

- [Permissions](https://meadowstack.com/patterns/permissions/) (Patterns)
- [Postgres: the person in a transaction setting](https://meadowstack.com/integrations/postgres/) (Integrations)
- [Supabase: the rules in the database](https://meadowstack.com/integrations/supabase/) (Integrations)
- [Row-level security (RLS)](https://meadowstack.com/glossary/row-level-security/) (Glossary)
- [From Bubble to an app in your own repository](https://meadowstack.com/from/bubble/) (Moving from)
- [Team and roles, with the rules in code](https://meadowstack.com/screens/roles-and-permissions/) (Screens)
