Re: How to grant a privilege on all tables or views or both of a database to someone?

From: "Paul Ogden" <pogden(at)claresco(dot)com>
To: "Eric Du" <duxy(at)CDSC(dot)COM(dot)CN>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to grant a privilege on all tables or views or both of a database to someone?
Date: 2002-03-07 18:55:51
Message-ID: NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

How to grant a privilege on all tables or views or both of a database to
someone?Here's what I did, using psql:

First you need to build the grant statements. This is done using select
statements against pg_class ( system catalog that stores name along with
other information about each table, view, sequence in your database ). The
output is passed to a file with the \o command. The file will contain the
GRANT statement for each object according to your search condition described
in the WHERE clause. Run the new file at the psql prompt with the \i
command and you are done. I saved both the psql command line script to
build the grant statements and the resulting sql to grant relevant
permission as system files that I can run any time I build or re-build a
database.

Connect to your database using psql as the user who owns the objects which
you want to grant permissions on. Be sure to set the appropriate permission
level by using the correct keyword ( one of Select, Update, Insert, Delete
or All ) after GRANT. Be sure you have write permissions to the local
directory where you are saving the resulting file(s):

\t # -- turns of headings
so the don't get in the file
\o /<local directory(ies)>/grants_tabtouser.sql
SELECT 'GRANT { SELECT | UPDATE | INSERT | DELETE | ALL } ON ' ||
relname || ' TO <username or keyword PUBLIC>;'
FROM pg_class
WHERE relkind ='r' # -- use = 'v' for views and =
'S' for sequences or IN ( 'r','v','S' ) for all
AND relowner IN ( # -- remove this clause to
generate grants on all objects. Must be superuser!
SELECT usesysid
FROM pg_user
WHERE usename = current_user )
\g # -- execute
\o # -- end output to
file
\t # -- turns headings
back on

then run the sql to issue the grants. Warning, you may wish to examine the
grants_tabtouser.sql file first using your favorite editor. Alternatively,
you could run the query in the first step without output to file turned on
and examine the results on the screen:

\i /<local directory(ies)>/grants_tabtouser.sql

Paul Ogden

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Eric Du
Sent: Thursday, March 07, 2002 02:38
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] How to grant a privilege on all tables or views or both of a
database to someone?

Is there a shortcut to grant a privilege on all tables or views or both of
a database instead of once a table or view?

Thanks in advance,

Eric Du
-----------------------------------
Tel: (86-010) 65544068-309
Fax: (86-010) 65544066

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message george young 2002-03-07 20:17:53 7.0.3 pg_dump -> segmentation fault!
Previous Message daq 2002-03-07 16:18:02 Re: PL/pgSQL Syntax Problem