2011-09-07 13 views
52

Come posso interrogare tutti i GRANTS concessi a un oggetto in postgres?Query concede una tabella in postgres

Per esempio io ho tabella "miatabella":

GRANT SELECT, INSERT ON mytable TO user1 
GRANT UPDATE ON mytable TO user2 

ho bisogno somthing che mi dà:

user1: SELECT, INSERT 
user2: UPDATE 

risposta

59

ho già trovato:

SELECT grantee, privilege_type 
FROM information_schema.role_table_grants 
WHERE table_name='mytable' 
71

\z mytable da psql ti dà tutte le sovvenzioni da una tabella, ma che ci si quindi devi dividerlo per singolo utente.

+0

vuoi eseguire questo direttamente dal riquadro SQL o riga di comando pg? –

+1

@ DanielL.VanDenBosch: tutti i meta-comandi, come '\ z', sono per psql. E psql è l'interfaccia a riga di comando di PostgreSQL. –

18

Se si vuole veramente una linea per ogni utente, è possibile raggruppare dal concessionario (richiede PG9 + per string_agg)

SELECT grantee, string_agg(privilege_type, ', ') AS privileges 
FROM information_schema.role_table_grants 
WHERE table_name='mytable' 
GROUP BY grantee; 

Questo dovrebbe produrre qualcosa di simile:

grantee | privileges 
---------+---------------- 
user1 | INSERT, SELECT 
user2 | UPDATE 
(2 rows) 
+1

Quasi quello che voglio, posso avere l'esatto 'GRANT' come uscite pg_dump? – brauliobo

8

Prova la query qui di seguito. Vi darà la lista di tutti gli utenti e le loro autorizzazioni sul tavolo.

select a.tablename,b.usename,HAS_TABLE_PRIVILEGE(usename,tablename, 'select') as select, 
    HAS_TABLE_PRIVILEGE(usename,tablename, 'insert') as insert, 
    HAS_TABLE_PRIVILEGE(usename,tablename, 'update') as update, 
    HAS_TABLE_PRIVILEGE(usename,tablename, 'delete') as delete, 
    HAS_TABLE_PRIVILEGE(usename,tablename, 'references') as references from pg_tables a , pg_user b 
where a.tablename='your_table_name'; 
+1

Questa è l'unica risposta qui che calcola le autorizzazioni ottenute dall'appartenenza ad altri ruoli, quindi ottiene il mio voto. D'altra parte, direi has_table_privilege (nome utente, contatto (schemaname, '.', Tablename), ...) 'per evitare ambiguità. –

+0

Più uno - QUESTO È ORO PURO! – Daniel

0

Ecco uno script che genera query di sovvenzione per una tabella specifica. Omette i privilegi del proprietario.

SELECT 
    format (
     'GRANT %s ON TABLE %I.%I TO %I%s;', 
     string_agg(tg.privilege_type, ', '), 
     tg.table_schema, 
     tg.table_name, 
     tg.grantee, 
     CASE 
     WHEN tg.is_grantable = 'YES' 
     THEN ' WITH GRANT OPTION' 
     ELSE '' 
     END 
    ) 
    FROM information_schema.role_table_grants tg 
    JOIN pg_tables t ON t.schemaname = tg.table_schema AND t.tablename = tg.table_name 
    WHERE 
    tg.table_schema = 'myschema' AND 
    tg.table_name='mytable' AND 
    t.tableowner <> tg.grantee 
    GROUP BY tg.table_schema, tg.table_name, tg.grantee, tg.is_grantable; 
2

Questa query elencherà tutte le tabelle in tutti i database e gli schemi (commento dalla riga (s) nella clausola WHERE per filtrare per specifici database, schemi o tabelle), con i privilegi mostrati in ordine in modo che sia facile da vedere se viene concesso un privilegio specifico o no: l'uscita

SELECT grantee 
     ,table_catalog 
     ,table_schema 
     ,table_name 
     ,string_agg(privilege_type, ', ' ORDER BY privilege_type) AS privileges 
FROM information_schema.role_table_grants 
WHERE grantee != 'postgres' 
-- and table_catalog = 'somedatabase' /* uncomment line to filter database */ 
-- and table_schema = 'someschema' /* uncomment line to filter schema */ 
-- and table_name = 'sometable' /* uncomment line to filter table */ 
GROUP BY 1, 2, 3, 4; 

Esempio:

grantee |table_catalog |table_schema |table_name  |privileges  | 
--------|----------------|--------------|---------------|---------------| 
PUBLIC |adventure_works |pg_catalog |pg_sequence |SELECT   | 
PUBLIC |adventure_works |pg_catalog |pg_sequences |SELECT   | 
PUBLIC |adventure_works |pg_catalog |pg_settings |SELECT, UPDATE | 
... 
Problemi correlati