pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSET ROLE and search_path
7+ messages / 5 participants
[nested] [flat]
* SET ROLE and search_path
@ 2020-05-20 16:36 Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 16:51 ` Re: SET ROLE and search_path Rob Sargent <robjsargent@gmail.com>
2020-05-20 19:28 ` Re: SET ROLE and search_path Adam Brusselback <adambrusselback@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Patrick FICHE @ 2020-05-20 16:36 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
@ 2020-05-20 16:51 ` Rob Sargent <robjsargent@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2020-05-20 16:51 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
@ 2020-05-20 19:28 ` Adam Brusselback <adambrusselback@gmail.com>
2020-05-20 19:32 ` Re: SET ROLE and search_path Rob Sargent <robjsargent@gmail.com>
2020-05-20 20:12 ` Re: SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
1 sibling, 2 replies; 7+ messages in thread
From: Adam Brusselback @ 2020-05-20 19:28 UTC (permalink / raw)
To: Patrick FICHE <Patrick.Fiche@aqsacom.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
I have this exact setup, and I use roles / schema names that match so the
$user var works with the search path when I set role as my application user.
> When search_path contains “$user”, does it refer to session_user or
current_user ?
It uses current_user, not session_user. Works perfectly with set_role for
me.
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 19:28 ` Re: SET ROLE and search_path Adam Brusselback <adambrusselback@gmail.com>
@ 2020-05-20 19:32 ` Rob Sargent <robjsargent@gmail.com>
2020-05-20 20:30 ` Re: SET ROLE and search_path Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 7+ messages in thread
From: Rob Sargent @ 2020-05-20 19:32 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 5/20/20 1:28 PM, Adam Brusselback wrote:
> I have this exact setup, and I use roles / schema names that match so
> the $user var works with the search path when I set role as my
> application user.
>
> > When search_path contains “$user”, does it refer to session_user or
> current_user ?
> It uses current_user, not session_user. Works perfectly with set_role
> for me.
>
Am I (again) alone in finding this a bit hokey? That a user name just
happens to be a schema name ...
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 19:28 ` Re: SET ROLE and search_path Adam Brusselback <adambrusselback@gmail.com>
2020-05-20 19:32 ` Re: SET ROLE and search_path Rob Sargent <robjsargent@gmail.com>
@ 2020-05-20 20:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-05-20 21:54 ` Re: SET ROLE and search_path Stephen Frost <sfrost@snowman.net>
0 siblings, 1 reply; 7+ messages in thread
From: Tom Lane @ 2020-05-20 20:30 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Rob Sargent <robjsargent@gmail.com> writes:
> Am I (again) alone in finding this a bit hokey? That a user name just
> happens to be a schema name ...
That's actually strongly encouraged by the SQL spec, if memory serves.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 19:28 ` Re: SET ROLE and search_path Adam Brusselback <adambrusselback@gmail.com>
2020-05-20 19:32 ` Re: SET ROLE and search_path Rob Sargent <robjsargent@gmail.com>
2020-05-20 20:30 ` Re: SET ROLE and search_path Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-05-20 21:54 ` Stephen Frost <sfrost@snowman.net>
0 siblings, 0 replies; 7+ messages in thread
From: Stephen Frost @ 2020-05-20 21:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Rob Sargent <robjsargent@gmail.com>; pgsql-general@lists.postgresql.org
Greetings,
* Tom Lane (tgl@sss.pgh.pa.us) wrote:
> Rob Sargent <robjsargent@gmail.com> writes:
> > Am I (again) alone in finding this a bit hokey? That a user name just
> > happens to be a schema name ...
>
> That's actually strongly encouraged by the SQL spec, if memory serves.
... and all-but-required by some other database systems. Doesn't make
it a good idea tho, imv.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: SET ROLE and search_path
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 19:28 ` Re: SET ROLE and search_path Adam Brusselback <adambrusselback@gmail.com>
@ 2020-05-20 20:12 ` Patrick FICHE <Patrick.Fiche@aqsacom.com>
1 sibling, 0 replies; 7+ messages in thread
From: Patrick FICHE @ 2020-05-20 20:12 UTC (permalink / raw)
To: Adam Brusselback <adambrusselback@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Thanks a lot
I will try it again. I probably missed something in my configuration.
Téléchargez Outlook pour iOS<https://aka.ms/o0ukef;
________________________________
De : Adam Brusselback <adambrusselback@gmail.com>
Envoyé : Wednesday, May 20, 2020 9:28:21 PM
À : Patrick FICHE <Patrick.Fiche@aqsacom.com>
Cc : pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Objet : Re: SET ROLE and search_path
I have this exact setup, and I use roles / schema names that match so the $user var works with the search path when I set role as my application user.
> When search_path contains “$user”, does it refer to session_user or current_user ?
It uses current_user, not session_user. Works perfectly with set_role for me.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2020-05-20 21:54 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-20 16:36 SET ROLE and search_path Patrick FICHE <Patrick.Fiche@aqsacom.com>
2020-05-20 16:51 ` Rob Sargent <robjsargent@gmail.com>
2020-05-20 19:28 ` Adam Brusselback <adambrusselback@gmail.com>
2020-05-20 19:32 ` Rob Sargent <robjsargent@gmail.com>
2020-05-20 20:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-05-20 21:54 ` Stephen Frost <sfrost@snowman.net>
2020-05-20 20:12 ` Patrick FICHE <Patrick.Fiche@aqsacom.com>
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