views:

88

answers:

2

I'm working on a web-based business application where each customer will need to have their own data (think basecamphq.com type model) For scalability and ease-of-upgrades, I'd prefer to have a single database where each customer gets a filtered version of the data. The problem is how to guarantee that they stay sandboxed to their own data. Trying to enforce it in code seems like a disaster waiting to happen. I know Oracle has a way to append a where clause to every query based on a login id, but does Postgresql have anything similar?

If not, is there a different design pattern I could use (like creating a view of each table for each customer that filters)?

Worse case scenario, what is the performance/memory overhead of having 1000 100M databases vs having a single 1Tb database? I will need to provide backup/restore functionality on a per-customer basis which is dead-simple on a single database but quite a bit trickier if they are sharing the database with other customers.

+3  A: 

You might want to look into adding Veil to your PostgreSQL installation.

Jerry Coffin
That looks like it could work. It does require a separate log-in for each user which would make the connection pooling more difficult. Do you have any experience running it in a production site?
John P
@John:No, not in production -- I was doing a consulting gig for a company a few months ago, and they considered the possibility, but decided to go a different route.
Jerry Coffin
I think you'd have to live with per customer login, otherwise any sandboxing would be futile, since it would be client or application side and direct access would break your sandbox.
MkV
A: 

Schemas plus inherited tables might work for this, create your master table then inherit tables into per-customer schemas which provide a company ID or name field default.

Set the permissions per schema for each customer and set the schema search path per user. Use the same table names in each schema so that the queries remain the same.

MkV
I found this presentation (http://aac2009.confreaks.com/06-feb-2009-14-30-writing-multi-tenant-applications-in-rails-guy-naor.html) about how basecamp did their schema separation. The way they did it was a before/after filter in the controller that would set the search path. My App is being written in java using the apache commons connection pool, so I'm not sure if it is viable. I'll check and see what options I have with it
John P