pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Ogden <pogden@claresco.com>
To: Eric Du <duxy@CDSC.COM.CN>
To: 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 10:55:51 -0800
Message-ID: <NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com> (raw)
In-Reply-To: <E732DB133C0B714DA3BBCA6AD19D27DB08266C@clarity.CDSC.COM.CN>

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@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

view thread (4+ messages)  latest in thread

Message-ID: <NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com>
Permalink:  ../NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com/
Also on:    postgresql.org/message-id/NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com

 · 

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: 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: <NAEOJBHEEOEHNNICGFADKENNCDAA.pogden@claresco.com>

* 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