My web app has multiple deployments -- each is a unique site with a unique URL.
Each deployment has different data, UI, etc. but a very similar Postgresql database structure (with PostGIS). The databases all live on the same DB server. I would like users from 1 deployment to be able to log in to all other deployed apps without having to re-register.
What I want is a single "users" table that is shared across multiple app databases. Any user who registers in one app should be recognized by all other apps. Is this possible with Postgres? Are schemas the way to do this?
The answer of filiprem is awesome.. However, I found that I needed to set up the paths also for the users
app01
,app02
:Also if you need that the role
app
to have access to the tables in the schemas app01, app02, you would need this code:Instead of
SELECT
you can have other privileges. The same for userapp02
.Update: In order to be able for the user
app
to select rows inapp01
schema, it needs at leastUSAGE
privilege for the schemaapp01
(along with table privilegeSELECT
defined above):Take a look at dblink.
Yes, schemas are the solution. Use a single PostgreSQL cluster, with a single database.
Create a group for all of app users:
Create global "app" schema, where all global shared applications tables will live.
Create separate user (with no superuser rights) for each of deployments:
Optionally, instead of
IN ROLE app
, you can grant explicit rights for these users on selected app objects:Create private schemas, where deployment-dependent tables will live:
Now you have a private schema for every application deployed; but at the same time you have shared access to global data.
What's nice, is that application does not have to be schema-aware.
SELECT * FROM froobles
will by default resolve toSELECT * FROM app01.froobles
, if you are connected asapp01
user. You do not have to specify schema name.As an extra measure, you can use table inheritance to extend global objects on per-deployment basis: