Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jbRwb-0002tv-Ox for pgsql-general@arkaria.postgresql.org; Wed, 20 May 2020 16:51:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jbRwZ-0005R7-Q1 for pgsql-general@arkaria.postgresql.org; Wed, 20 May 2020 16:51:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jbRwZ-0005Qx-Ek for pgsql-general@lists.postgresql.org; Wed, 20 May 2020 16:51:47 +0000 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jbRwS-0007HQ-Vt for pgsql-general@lists.postgresql.org; Wed, 20 May 2020 16:51:47 +0000 Received: by mail-ot1-x341.google.com with SMTP id 69so3044580otv.2 for ; Wed, 20 May 2020 09:51:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=oeNxSCW5E3Z2zyWarhXoOF3ReC36sxA4bL4Engn2bjM=; b=KTV8qIBsbzHK/EcRMV3FEYhLbsQxXgKHP/u+diOcj8FylK68GhsQVrXLVtF8kVFVVR NgyRk1kyVgOoFlXxWaGbrRrBMt9USPezLPqtHDNsZJBQDas2rpLHKiICND4Atd+3W2N5 tfvI4f7Y5Uu5VYmvmEfSGTZBzCgs5H8Nk3W294R3S9V8W659YGZRrLSo1Bky7DY+IgA6 agNH6ObAuIGc9+FlmhbI7BrH+AmG28rM9N7ojncMNTDGFWD12hFtBwfIBiS4WmN+9DGx nkAgFG1tFdWHYYFlBsGwOQ3iRi/I4DRMz/FFI2aL6Vnfphsey0q5WdkWgGx5sq7wWSvR 5wjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=oeNxSCW5E3Z2zyWarhXoOF3ReC36sxA4bL4Engn2bjM=; b=NR4ZN33hhUpjk8yarm80h/QfoZ8rs0i4RsrGhjQNay47Tvf1F98DzCe/kqHSDd7q4X C7APJbjeRei64amRfDEF357uP6AxWz7KQ+MRZrlynhrubKRNT3A5odxHpnh9CWm6ApPg I7ZLQNKZGGck+EyeBFdJ0nnfzTPtKRNn3jQU7Z6LYPWAUDwfawi6spHNAXxnTl+OVb5D AXp/DXcQyeg1HIFFvh5bTjv1cBm4AcKfpYCwvV5nQMctt59XgcrY57XZYmBD1Q/EiPrj ABXZVqAZldxF5J75bWeW3+pCkXOheTAkIctWMf02QnhSprpMkX0lyTIMIUUv7AClmXgZ wMKA== X-Gm-Message-State: AOAM533Ty9opU+VaYbHv5Pkd9aLPiPAqDuzl3MuWFg8JV7o9dRX+ctDA p9LEPecglqORKXX/jpSBPUaODQpT X-Google-Smtp-Source: ABdhPJxDl+h69Oc8C4RI6RH342qTV647mOcxuaf3/MNYZxdP+OL9RU8mIWGWYNvFcNfjnCUvWWD8gg== X-Received: by 2002:a9d:600e:: with SMTP id h14mr4036908otj.281.1589993498890; Wed, 20 May 2020 09:51:38 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:bde2:bf5e:1c88:2238? ([2601:681:5500:dde0:bde2:bf5e:1c88:2238]) by smtp.gmail.com with ESMTPSA id v24sm849402otn.59.2020.05.20.09.51.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 20 May 2020 09:51:37 -0700 (PDT) Subject: Re: SET ROLE and search_path To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <47761b8b-d892-943b-1bc9-29bde61a2e62@gmail.com> Date: Wed, 20 May 2020 10:51:36 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------4D73812832E879528745560F" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------4D73812832E879528745560F Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit 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. --------------4D73812832E879528745560F Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit

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.

--------------4D73812832E879528745560F--