pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: SET ROLE and search_path
Date: Wed, 20 May 2020 10:51:36 -0600
Message-ID: <47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com> (raw)
In-Reply-To: <AM6PR05MB5287F42DF5C317F2BDA42E43EFB60@AM6PR05MB5287.eurprd05.prod.outlook.com>
References: <AM6PR05MB5287F42DF5C317F2BDA42E43EFB60@AM6PR05MB5287.eurprd05.prod.outlook.com>



On 5/20/20 10:36 AM, Patrick FICHE wrote:
>
> Hi,
>
> I’m trying to implement a PostgreSQL multi-tenant database that will 
> be accessed by a Web Application.
>
> The users that will login will belong to different companies and a 
> schema was created in the database for each company.
>
> However, I would like the Web Application to connect with a single 
> Postgres login.
>
> Let’s say that I have 2 companies : comp1 and comp2 with their 
> respective schema (comp1 / comp2).
>
> Then, the web application connects with web_app login which has been 
> granted comp1 and comp2 roles….
>
> Depending on the user connecting to the application, I would like to 
> use SET ROLE comp1 / SET ROLE comp2 in order to get access to the 
> relevant data only.
>
> However, it seems that SET ROLE does not change the search_path (which 
> is different for comp1 and comp2).
>
> Is there any way to change the search_path in an easy way (in a 
> procedure) after SET ROLE has been executed.
>
> Am I missing anything with SET ROLE.
>
> When search_path contains “$user”, does it refer to session_user or 
> current_user ?
>
> Thanks for any advice
>
> Patrick
>
Does your role definition assign a search_path?

    create role comp1;
    alter role comp1 set search_path=comp1,base,public;

Every re-use of the postgres connection must start by resetting the 
search_path.  I find it easier to log in as comp1.  Some jiggery-pokery 
involved in passwords but no one in company #1 needs to know the user 
name let alone password.

view thread (7+ messages)  latest in thread

Message-ID: <47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com>
Permalink:  ../47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com/
Also on:    postgresql.org/message-id/47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: SET ROLE and search_path
  In-Reply-To: <47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com>
  References: <AM6PR05MB5287F42DF5C317F2BDA42E43EFB60@AM6PR05MB5287.eurprd05.prod.outlook.com> <47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox