Multi-tenancy
 with PostgreSQL
Requirements
Three ways
bit.ly/msn-multitenant-arch
Shared Database, Shared Namespace
Separate Databases
Shared Database,
Separate Namespace
has
schemas
has
namespaces
Guy Naor on Multi-tenancy
http://bit.ly/guy-naor-multitenant-2009
App
carding.app.com/products/1-grapes
SET SEARCH_PATH = 'carding'
'carding'


SELECT * FROM products
where id = 1;
Common Tables
"public"
'carding'


SELECT * FROM public.invoices
where tenant_id = 1;
Apartment
github.com/bradrobertson/apartment
bob.sql




carding




alice.sql
carding
carding   assimo
assimo
Storey
github.com/ramontayag/storey
Gotchas
Problem: Testing
●   transactions do not play well with
    schemas
Answer: Testing
●   in tests, treat the public schema as a
    tenant
Problem: Migrations
●   migrate every schema
Answer: Apartment/Storey
●   loops through the namespaces and
    migrates each one
Problem: Background Jobs
●   how do the workers know which
    schema contains the data?
Answer: Pass schema name
 ●   Pass the schema name along with the
     rest of the job arguments
Questions?

Multi tenancy with PostgreSQL