Multi-Tenancy and Shared Customers in PostgreSQL

database-designmulti-tenantpostgresql

I am developing multi-tenancy application and I have no problem with the architecture itself. I find difficult on how to make customers table shareable among tenants. This is because same customer can subscribe to multiple tenants. I don't want to have same duplicate data and so user should register only once and be able to subscribe to any of tenant with the same credentials.

I have tried to look over the internet but could not find any good resource (may be I am using wrong keywords). I would appreciate any help (including the architecture name if any) to solve this problem.

If I am not clear please let me know.

Best Answer

Multi-tenancy can be achieved in several ways; opaque multi-tenancy doesn't necessarily mean a design with one schema per tenant. It can also be achieved by adding a "tenant ID" column to every single table. As an example, this is the way that SAP has done it for a very long time already.

Applying this principle to your design, you would add "tenant ID" to all tables that need to be multi-tenant, while the "customers" table won't have it. Instead you need to add a "tenant_customers" table with two columns; "tenant ID" and "customer ID" to associate customers with tenants. Wikipedia calls this concept an Associative Entity.

Alternatively, you can store your tenant-specific data in one schema per tenant and have the customer table and "tenant_customers" associative table stored in its own schema. Instead of "tenant ID" you could use a string storing the "tenant schema name".