pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew G. Hammond <drew@xyzzy.dhs.org>
To: Paul Ogden <pogden@claresco.com>
Cc: Eric Du <duxy@CDSC.COM.CN>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to grant a privilege on all tables or views or both of a database to someone?
Date: Thu, 7 Mar 2002 15:40:59 -0500
Message-ID: <20020307204059.GA22890@xyzzy.dhs.org> (raw)
In-Reply-To: <NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com>
References: <E732DB133C0B714DA3BBCA6AD19D27DB08266C@clarity.CDSC.COM.CN>
	<NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com>

On Thu, Mar 07, 2002 at 10:55:51AM -0800, Paul Ogden wrote:
> How to grant a privilege on all tables or views or both of a database to
> someone?Here's what I did, using psql:

Personally I solved this using plpgsql:

-- usage: SELECT grant_all('privilidge', 'user');
-- grants privilidge (SELECT, UPDATE, INSERT, DELETE or ALL) to the user
-- for all non-pg_ objects in the database (except indices).

DROP FUNCTION grant_all(text, text);
CREATE FUNCTION grant_all(text, text) RETURNS boolean AS '
DECLARE item RECORD;
  priv ALIAS FOR $1;
  user ALIAS FOR $2;
BEGIN 
  FOR item IN SELECT * FROM pg_class 
    WHERE relname !~ ''^pg_'' 
    AND relkind <> ''i''
  LOOP
    EXECUTE ''GRANT '' || priv 
      || '' ON '' || quote_ident(item.relname)
      || '' TO '' || quote_ident(user);
  END LOOP;
  RETURN ''t'';
END;' LANGUAGE 'plpgsql';

DROP FUNCTION grant_all_tables(text, text);
CREATE FUNCTION grant_all_tables(text, text) RETURNS boolean AS '
DECLARE item RECORD;
  priv ALIAS FOR $1;
  user ALIAS FOR $2;
BEGIN
  FOR item IN SELECT * FROM pg_class 
    WHERE relname !~ ''^pg_''
    AND relkind = ''r''
  LOOP
    EXECUTE ''GRANT '' || priv
      || '' ON '' || quote_ident(item.relname)
      || '' TO '' || quote_ident(user);
  END LOOP;
  RETURN ''t'';
END;' LANGUAGE 'plpgsql';

DROP FUNCTION grant_all_tables_and_views(text, text);
CREATE FUNCTION grant_all_tables_and_views(text, text) RETURNS boolean AS '
DECLARE item RECORD;
  priv ALIAS FOR $1;
  user ALIAS FOR $2;
BEGIN
  FOR item IN SELECT * FROM pg_class
    WHERE relname !~ ''^pg_''
    AND relkind  IN (''r'', ''v'')
  LOOP
    EXECUTE ''GRANT '' || priv
      || '' ON '' || quote_ident(item.relname)
      || '' TO '' || quote_ident(user);
  END LOOP;
  RETURN ''t'';
END;' LANGUAGE 'plpgsql';

-- 
Andrew G. Hammond  mailto:drew@xyzzy.dhs.org  http://xyzzy.dhs.org/~drew/
56 2A 54 EF 19 C0 3B 43 72 69 5B E3 69 5B A1 1F              613-389-5481 
5CD3 62B0 254B DEB1 86E0  8959 093E F70A B457 84B1
"To blow recursion you must first blow recur" -- me

view thread (4+ messages)  latest in thread

Message-ID: <20020307204059.GA22890@xyzzy.dhs.org>
Permalink:  ../20020307204059.GA22890@xyzzy.dhs.org/
Also on:    postgresql.org/message-id/20020307204059.GA22890@xyzzy.dhs.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: drew@xyzzy.dhs.org, pogden@claresco.com, duxy@CDSC.COM.CN
  Subject: Re: How to grant a privilege on all tables or views or both of a database to someone?
  In-Reply-To: <20020307204059.GA22890@xyzzy.dhs.org>

* 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