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 1q5QfN-0000SV-SK for pgsql-general@arkaria.postgresql.org; Sat, 03 Jun 2023 12:47:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q5QfM-0004vt-LP for pgsql-general@arkaria.postgresql.org; Sat, 03 Jun 2023 12:47:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q5QeI-0008NM-7m for pgsql-general@lists.postgresql.org; Sat, 03 Jun 2023 12:46:26 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q5QeF-003QR7-Hx for pgsql-general@postgresql.org; Sat, 03 Jun 2023 12:46:25 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4QYKP54wdsz9sQL; Sat, 3 Jun 2023 14:46:17 +0200 (CEST) Date: Sat, 3 Jun 2023 14:46:16 +0200 (CEST) From: Erik Wienhold To: Andrus , pgsql-general Message-ID: <2140448238.796716.1685796376553@office.mailbox.org> In-Reply-To: <568e6761-d224-7c72-59f3-267b39bde227@hot.ee> References: <568e6761-d224-7c72-59f3-267b39bde227@hot.ee> Subject: Re: How to remove user specific grant and revoke MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4QYKP54wdsz9sQL List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 03/06/2023 09:16 CEST Andrus wrote: > > User groups table is defined as > > CREATE TABLE IF NOT EXISTS public.kaspriv > ( > id serial primary key, > user character(10) NOT NULL, > group character(35) NOT NULL > ... > ) > > There are hundreds of users. Earlier time grant and revoke commands were > executed for every user separately. Later revoke and grant commands for > public were added: > > REVOKE ALL ON TABLE public.kaspriv FROM PUBLIC; > GRANT SELECT ON TABLE public.kaspriv TO PUBLIC; > > pgAdmin SQL tab still shows revoke and grant commands for every user also: > > REVOKE ALL ON TABLE public.kaspriv FROM PUBLIC; > REVOKE ALL ON TABLE public.kaspriv FROM someuser; > REVOKE ALL ON TABLE public.kaspriv FROM someotheruser; > ... > GRANT SELECT ON TABLE public.kaspriv TO PUBLIC; > GRANT SELECT ON TABLE public.kaspriv TO someuser; > GRANT SELECT ON TABLE public.kaspriv TO someother; > ... > > How to remove those unnecessary user-specific GRANT and REVOKE commands to > make rights cleaner? pgAdmin does not have delete option for those. When you run REVOKE SELECT ON TABLE public.kaspriv FROM someuser; does it also remove the accompanying REVOKE ALL statement for that user? That REVOKE SELECT should remove the ACL for someuser from pg_class.relacl and pgAdmin should no longer find any ACL for that role and thus no longer emit REVOKE ALL. > Something like > > DROP REVOKE ALL ON TABLE public.kaspriv FROM all EXCEPT public; > DROP GRANT SELECT ON TABLE public.kaspriv FROM all EXCEPT public; > > This will be one-time action. It can be done manually in pgadmin or using > some script running once. Automate this with aclexplode[0] to get the privileges for specific grantees. Loop over the result set in a DO block, generate the REVOKE commands, and EXECUTE them. SELECT acl.grantee::regrole, acl.privilege_type FROM pg_class, aclexplode(relacl) acl WHERE oid = 'public.kaspriv'::regclass; > Using > > PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, > compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit > > and latest pgAdmin 7.2 [0] https://www.postgresql.org/docs/12/functions-info.html#FUNCTIONS-ACLITEM-FN-TABLE -- Erik