Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1feHqM-0008Fc-3I for pgsql-general@arkaria.postgresql.org; Sat, 14 Jul 2018 10:32:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1feHqJ-00029Y-K7 for pgsql-general@arkaria.postgresql.org; Sat, 14 Jul 2018 10:31:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1feHqJ-00029R-9d for pgsql-general@lists.postgresql.org; Sat, 14 Jul 2018 10:31:59 +0000 Received: from aragorn.net-wizard.org ([138.201.127.29] helo=gandalf.net-wizard.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1feHqE-0001ur-IA for pgsql-general@postgresql.org; Sat, 14 Jul 2018 10:31:57 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id EDC4380B47; Sat, 14 Jul 2018 12:31:50 +0200 (CEST) X-Virus-Scanned: Gentoo amavisd-new at aragorn Received: from aragorn.net-wizard.org ([127.0.0.1]) by localhost (aragorn.net-wizard.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GpCQ9MNWDAbl; Sat, 14 Jul 2018 12:31:46 +0200 (CEST) Received: from MTEC0211 (mtec-lee-dock-2-133.ethz.ch [129.132.6.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: clavadetscher@swisspug.org) by gandalf.net-wizard.org (Postfix) with ESMTPSA id 4DD0C80B33; Sat, 14 Jul 2018 12:31:42 +0200 (CEST) From: "Charles Clavadetscher" To: =?utf-8?Q?'=C5=81ukasz_Jarych'?= , References: <02ef01d41b3b$1e2fda60$5a8f8f20$@swisspug.org> In-Reply-To: Subject: RE: Read only to schema Date: Sat, 14 Jul 2018 12:31:36 +0200 Message-ID: <031b01d41b5d$dd73a880$985af980$@swisspug.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_031C_01D41B6E.A0FEC270" X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQHznPPGGnJz+lyr/RH7IZtKJgF+kQJgXwL7AeXPFJoBa9x9KQGVeTgWpBVCufA= Content-Language: de-ch List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_031C_01D41B6E.A0FEC270 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hello Jacek =20 From: =C5=81ukasz Jarych [mailto:jaryszek@gmail.com]=20 Sent: Samstag, 14. Juli 2018 11:55 To: clavadetscher@swisspug.org; pgsql-general@postgresql.org >> = PG-General Mailing List Subject: Re: Read only to schema =20 Thank you very much Charles!=20 =20 Awesome knowledge, thank you! =20 I will test it and let you know if it is working like a charm (i am on = vacation now and without access to postgresql). =20 I am wondering with one thing:=20 =20 GRANT CREATE ON SCHEMA PUBLIC TO jaryszek; =20 This will allow to create, drop, isnert and delete? All ddl and dml = commands?=20 Or should i use GRANT ALL ON SCHEMA PUBLIC TO jaryszek ? =20 No. There are 2 privileges that you can grant on a schema: USAGE: Allows to use objects in that schema. CREATE: Allows to create and destroy objects in that schema. =20 None of those says anything about which privileges users have within the = schema. You need to define additionally privileges (e.g. select, insert, = update, delete) on the objects within the schema. =20 So in order to get access to a table public.test a user must have: =20 USAGE on schema public AND SELECT (or whatever) on the table itself. If = any of those is missing the user will not be able to access the table. =20 GRANT ALL is generally a bad idea, althought on schemas there is not = much you can do wrong. It doesn=E2=80=99t hurt if you add USAGE (which would be included in ALL = along CREATE) for user jaryszek, but it is not necessary, because public = (=3D any user) was only revoked CREATE (s. example in last mail). That = means public still has USAGE on schema public and obviously you are on = one of =E2=80=9Cany user=E2=80=9D. =20 I hope I could explain that somehow. If you still have questions just get back on the list. =20 Bye Charles =20 Best, Jacek=20 =20 sob., 14 lip 2018 o 08:23 Charles Clavadetscher = > = napisa=C5=82(a): Hello =20 From: =C5=81ukasz Jarych [mailto:jaryszek@gmail.com = ]=20 Sent: Freitag, 13. Juli 2018 16:39 To: pgsql-general@postgresql.org = >> PG-General Mailing List > Subject: Re: Read only to schema =20 I found something like this: =20 CREATE ROLE readonly_user WITH LOGIN ENCRYPTED PASSWORD '1234' =20 ALTER ROLE readonly_user SET search_path to=20 public =20 GRANT CONNECT ON DATABASE "TestDb" TO readonly_user; =20 GRANT USAGE ON SCHEMA public TO readonly_user; =20 GRANT USAGE ON ALL SEQUENCES -- Alternatively: ON SEQUENCE seq1, seq2, seq3 ... IN SCHEMA public TO readonly_user; =20 GRANT SELECT ON ALL TABLES -- Alternatively: ON TABLE table1, view1, table2 ... IN SCHEMA public TO readonly_user; =20 Question is how to give this user opposite access? I mean give him = access to all functionalities like inserting, deleting, creating tables = and staff like this.=20 =20 I mean i want to assign user "jaryszek" to this read_only role and after = changing schema i want to give user "jaryszek" all credentials.=20 =20 Best, Jacek=20 =20 =20 You can change your readonly_user to NOINHERIT and GRANT the role to = jaryszek. When you then want to act as readonly_user you set the role explicitly. =20 Here basically: =20 Revoke create from public, so that only granted users will be able to = create or drop objects. REVOKE CREATE ON SCHEMA PUBLIC FROM public; =20 Create the role as group (nologin) and without implicit inheritance of = privileges. CREATE ROLE readonly_user NOINHERIT NOLOGIN; =20 Your normal user should be able to create tables. GRANT CREATE ON SCHEMA PUBLIC TO jaryszek; =20 Add your user to the readonly_user group. GRANT readonly_user TO jaryszek; =20 Now when you log in as jaryszek you can create table add data, etc. =20 jaryszek@db.localhost =3D> SELECT = SESSION_USER, CURRENT_USER; session_user | current_user --------------+-------------- jaryszek | jaryszek =20 jaryszek@db.localhost =3D> CREATE TABLE = public.test (a INTEGER); CREATE TABLE jaryszek@db.localhost =3D> INSERT INTO = public.test VALUES (1); INSERT 0 1 jaryszek@db.localhost =3D> SELECT * FROM = public.test; a --- 1 (1 row) =20 Now let=E2=80=99s set up the permissions of readonly_user. =20 GRANT SELECT ON ALL TABLES IN SCHEMA PUBLIC TO readonly_user; =20 When you want to act as readonly_user you set explicitly that role. =20 jaryszek@db.localhost =3D> SET ROLE = readonly_user ; SET jaryszek@db.localhost =3D> SELECT = SESSION_USER, CURRENT_USER; session_user | current_user --------------+--------------- jaryszek | readonly_user (1 row) =20 After this all privileges will be checked against readonly_user. That = means: =20 You can read from tables, but you cannot modify data or change/create = tables. =20 jaryszek@db.localhost =3D> SELECT * FROM = public.test; a --- 1 (1 row) =20 jaryszek@db.localhost =3D> INSERT INTO = public.test VALUES (2); ERROR: permission denied for relation test =20 jaryszek@db.localhost =3D> CREATE TABLE = public.test2 (a INTEGER); ERROR: permission denied for schema public LINE 1: CREATE TABLE public.test2 (a INTEGER); =20 When you want to get back to your normal role then use =20 jaryszek@db.localhost =3D> RESET ROLE; RESET jaryszek@db.localhost =3D> INSERT INTO = public.test VALUES (2); INSERT 0 1 =20 The idea is to put all permissions in (group) roles and then impersonate = the role that you need setting it explicitly. =20 I hope this helps. Bye Charles =20 =20 pt., 13 lip 2018 o 12:58 =C5=81ukasz Jarych > napisa=C5=82(a): Maybe read-only view? =20 Best, Jacek=20 =20 pt., 13 lip 2018 o 07:00 =C5=81ukasz Jarych > napisa=C5=82(a): Hi Guys, =20 Yesterday i tried all day to figure out system to read only schemas.=20 =20 I want to : =20 1. Create user who can login (user: jaryszek) 2. Create role who can read only data (only watching tables) (role: = readonly) 3, Create role who can read all data (inserting, deleting, altering, = dropping) (role: readall) =20 What sqls should i use for this?=20 What grants should i add? =20 And now i am logged as jaryszek =20 I want to grant myself role read only to schema public (when owner is = postgres).=20 I want to review tables as views only, After work i want to grant myself role readall to schema public.=20 =20 It is possible?=20 Or possible workaround ?=20 =20 Best, Jacek ------=_NextPart_000_031C_01D41B6E.A0FEC270 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Hello = Jacek

 

From:<= /b> =C5=81ukasz = Jarych [mailto:jaryszek@gmail.com]
Sent: Samstag, 14. Juli = 2018 11:55
To: clavadetscher@swisspug.org; = pgsql-general@postgresql.org >> PG-General Mailing List = <pgsql-general@postgresql.org>
Subject: Re: Read only to = schema

 

Thank = you very much Charles! 

 

Awesome knowledge, thank = you!

 

I = will test it and let you know if it is working like a charm (i am on = vacation now and without access to = postgresql).

 

I = am wondering with one thing: 

 

GRANT CREATE ON = SCHEMA PUBLIC TO jaryszek;

 

This will allow to create, drop, = isnert and delete? All ddl and dml = commands? 

Or should = i use GRANT ALL ON SCHEMA PUBLIC TO jaryszek = ?

 

No. = There are 2 privileges that you can grant on a = schema:

USAGE: = Allows to use objects in that schema.

CREATE: Allows to create and destroy objects in that = schema.

 

None = of those says anything about which privileges users have within the = schema. You need to define additionally privileges (e.g. select, insert, = update, delete) on the objects within the = schema.

 

So in = order to get access to a table public.test a user must = have:

 

USAGE = on schema public AND SELECT (or whatever) on the table itself. If any of = those is missing the user will not be able to access the = table.

 

GRANT = ALL is generally a bad idea, althought on schemas there is not much you = can do wrong.

It doesn=E2=80=99t hurt if you add USAGE (which = would be included in ALL along CREATE) for user jaryszek, but it is not = necessary, because public (=3D any user) was only revoked CREATE (s. = example in last mail). That means public still has USAGE on schema = public and obviously you are on one of =E2=80=9Cany = user=E2=80=9D.

 

I hope = I could explain that somehow.

If you = still have questions just get back on the list.

 

Bye

Charles

 

Best,

Jacek 

 

sob., 14 lip 2018 o 08:23 Charles Clavadetscher = <clavadetscher@swisspug.org= > napisa=C5=82(a):

Hello

 

From:<= /b> =C5=81ukasz = Jarych [mailto:jaryszek@gmail.com]
Sent: Freitag, 13. = Juli 2018 16:39
To: pgsql-general@postgresql.org >> PG-General = Mailing List <pgsql-general@postgresql.org>
Subject:= Re: Read only to schema

 <= /o:p>

I found = something like this:

 <= /o:p>

CREATE ROLE = readonly_user

  =      WITH LOGIN

  =      ENCRYPTED PASSWORD = '1234'

  =           =  

ALTER ROLE = readonly_user

SET = search_path to 

public<= /o:p>

  =           =  

 GRANT = CONNECT

  =   ON DATABASE "TestDb"

  =   TO readonly_user;

  =        

 GRANT = USAGE

  =   ON SCHEMA public

  =   TO readonly_user;

  =        

GRANT = USAGE

  =   ON ALL SEQUENCES  -- Alternatively: ON SEQUENCE seq1, seq2, = seq3 ...

  =   IN SCHEMA public

  =   TO readonly_user;

  =        

 GRANT = SELECT

  =   ON ALL TABLES  -- Alternatively: ON TABLE table1, view1, = table2 ...

  =   IN SCHEMA public

  =   TO readonly_user;

 <= /o:p>

Question is = how to give this user opposite access? I mean give him access to all = functionalities like inserting, deleting, creating tables and staff like = this. 

 <= /o:p>

I mean i = want to assign user "jaryszek" to this read_only role and = after changing schema i want to give user "jaryszek" all = credentials. 

 <= /o:p>

Best,

Jacek <= o:p>

 <= /o:p>

 <= /o:p>

You can change your readonly_user = to NOINHERIT and GRANT the role to jaryszek.

When you then want to act as = readonly_user you set the role explicitly.

 

Here = basically:

 

Revoke create from public, so that = only granted users will be able to create or drop = objects.

REVOKE CREATE ON SCHEMA PUBLIC FROM = public;

 

Create the role as group (nologin) = and without implicit inheritance of privileges.

CREATE ROLE readonly_user NOINHERIT = NOLOGIN;

 

Your normal user should be able to = create tables.

GRANT CREATE ON SCHEMA PUBLIC TO = jaryszek;

 

Add your user to the readonly_user = group.

GRANT readonly_user TO = jaryszek;

 

Now when you log in as jaryszek you = can create table add data, etc.

 

jaryszek@db.localhost=3D> = SELECT SESSION_USER, CURRENT_USER;

session_user | = current_user

--------------+--------------

jaryszek     | = jaryszek

 

jaryszek@db.localhost=3D> = CREATE TABLE public.test (a INTEGER);

CREATE = TABLE

jaryszek@db.localhost=3D> = INSERT INTO public.test VALUES (1);

INSERT 0 = 1

jaryszek@db.localhost=3D> = SELECT * FROM public.test;

a

---

1

(1 row)

 

Now let=E2=80=99s set up the = permissions of readonly_user.

 

GRANT SELECT ON ALL TABLES IN = SCHEMA PUBLIC TO readonly_user;

 

When you want to act as = readonly_user you set explicitly that role.

 

jaryszek@db.localhost=3D> = SET ROLE readonly_user ;

SET

jaryszek@db.localhost=3D> = SELECT SESSION_USER, CURRENT_USER;

session_user | = current_user

--------------+---------------<= /p>

jaryszek     | = readonly_user

(1 row)

 

After this all privileges will be = checked against readonly_user. That means:

 

You can read from tables, but you = cannot modify data or change/create tables.

 

jaryszek@db.localhost=3D> = SELECT * FROM public.test;

a

---

1

(1 = row)

 

jaryszek@db.localhost=3D> = INSERT INTO public.test VALUES (2);

ERROR:  = permission denied for relation test

 

jaryszek@db.localhost=3D> = CREATE TABLE public.test2 (a INTEGER);

ERROR:  = permission denied for schema public

LINE 1: CREATE TABLE public.test2 = (a INTEGER);

 

When you want to get back to your = normal role then use

 

jaryszek@db.localhost=3D> = RESET ROLE;

RESET

jaryszek@db.localhost=3D> = INSERT INTO public.test VALUES (2);

INSERT 0 1

 

The idea is to put all permissions = in (group) roles and then impersonate the role that you need setting it = explicitly.

 

I hope this helps.

Bye

Charles

 

 

pt., 13 lip = 2018 o 12:58 =C5=81ukasz Jarych <jaryszek@gmail.com> = napisa=C5=82(a):

Maybe = read-only view?

 <= /o:p>

Best,

Jacek <= o:p>

 <= /o:p>

pt., 13 lip = 2018 o 07:00 =C5=81ukasz Jarych <jaryszek@gmail.com> = napisa=C5=82(a):

Hi = Guys,

 <= /o:p>

Yesterday i = tried all day to figure out system to read only = schemas. 

 <= /o:p>

I want to = :

 <= /o:p>

1. Create = user who can login (user: jaryszek)

2. Create = role who can read only data (only watching tables) (role: = readonly)

3, Create = role who can read all data (inserting, deleting, altering, dropping) = (role: readall)

 <= /o:p>

What sqls = should i use for this? 

What grants = should i add?

 <= /o:p>

And now i = am logged as jaryszek

 <= /o:p>

I want to = grant myself role read only to schema public (when owner is = postgres). 

I want to = review tables as views only,

After work = i want to grant myself role readall to schema = public. 

 <= /o:p>

It is = possible? 

Or possible = workaround ? 

 <= /o:p>

Best,

Jacek

------=_NextPart_000_031C_01D41B6E.A0FEC270--