How to share a table between multiple Postgresql databases

前端 未结 3 817
时光取名叫无心
时光取名叫无心 2021-02-02 16:59

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

3条回答
  •  面向向阳花
    2021-02-02 17:37

    The answer of filiprem is awesome.. However, I found that I needed to set up the paths also for the users app01, app02:

    ALTER USER app01 SET SEARCH_PATH TO "$user",app;
    ALTER USER app02 SET SEARCH_PATH TO "$user",app;
    

    Also if you need that the role app to have access to the tables in the schemas app01, app02, you would need this code:

    -- grant all future tables
    ALTER DEFAULT PRIVILEGES IN SCHEMA app01 GRANT SELECT ON TABLES TO app;
    
    -- grant all existing tables
    GRANT SELECT 
    ON ALL TABLES IN SCHEMA app01
    TO app
    

    Instead of SELECT you can have other privileges. The same for user app02.

    Update: In order to be able for the user app to select rows in app01 schema, it needs at least USAGE privilege for the schema app01 (along with table privilege SELECT defined above):

    GRANT USAGE ON SCHEMA app01 TO app; 
    

提交回复
热议问题