Re: How to grant a privilege on all tables or views or both of a database to someone? - Mailing list pgsql-sql

From Paul Ogden
Subject Re: How to grant a privilege on all tables or views or both of a database to someone?
Date
Msg-id NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com
Whole thread Raw
In response to How to grant a privilege on all tables or views or both of a database to someone?  ("Eric Du" <duxy@CDSC.COM.CN>)
Responses Re: How to grant a privilege on all tables or views or both of a database to someone?
List pgsql-sql
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@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Eric Du
Sent: Thursday, March 07, 2002 02:38
To: pgsql-sql@postgresql.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

pgsql-sql by date:

Previous
From: daq
Date:
Subject: Re: PL/pgSQL Syntax Problem
Next
From: george young
Date:
Subject: 7.0.3 pg_dump -> segmentation fault!