2011-07-23 66 views
14

我無法弄清楚如何給特定用戶的每一個正確的,我希望用戶對架構的每一個正確的:PGSQL:授予對架構一個用戶的每一個正確的

  • 插入,刪除,更新,選擇,...現有表

我曾嘗試做:

GRANT ALL PRIVILEGES ON SCHEMA schema to "user"; 
    GRANT ALL ON SCHEMA schema to "local_518561"; 
    GRANT ALL PRIVILEGES ON table schema.table to "user"; 
    GRANT ALL ON table schema.table to "user"; 

的querys返回succesfull,但每次我用其他的用戶,我得到insuffie分權限錯誤。

回答

3

答案在於序列中,如果您沒有授予表格和序列的權限(如果有)而不能插入。

4

如果您使用的是版本的PostgreSQL < 9,你可以使用下面的存儲過程來管理表和序列的權限:

CREATE OR REPLACE FUNCTION grantTablesOfSchema (user VARCHAR, 
    permissions VARCHAR, schema VARCHAR) RETURNS VARCHAR AS 
$body$ 
DECLARE 
    regActual RECORD; 
    numTables INTEGER; 
BEGIN 

    numTables := 0; 

    FOR regActual IN 
     SELECT tablename FROM pg_tables WHERE schemaname = schema 
    LOOP 
     numTables := numTables + 1; 

     EXECUTE 'GRANT ' || permissions || ' ON ' || schema || '.' || regActual.tablename || ' TO ' || user; 
    END LOOP; 

    RETURN 'Tables: ' || numTables::VARCHAR; 
END; 
$body$ 
LANGUAGE 'plpgsql'; 


CREATE OR REPLACE FUNCTION grantSequencesOfSchema (user VARCHAR, 
    permissions VARCHAR, database VARCHAR, schema VARCHAR) RETURNS VARCHAR AS 
$body$ 
DECLARE 
    regActual  RECORD; 
    numSequences INTEGER; 
BEGIN 

    numSequences := 0; 

    FOR regActual IN 
     SELECT sequence_catalog, sequence_schema, sequence_name 
     FROM information_schema.sequences 
     WHERE sequence_catalog = database AND sequence_schema = schema 
    LOOP 
     numSequences := numSequences + 1; 

     EXECUTE 'GRANT ' || permissions || ' ON ' || schema || '.' || regActual.sequence_name || ' TO ' || user; 
    END LOOP; 

    RETURN 'Sequences: ' || numSequences::VARCHAR; 
END; 
$body$ 
LANGUAGE 'plpgsql'; 

,並使用一個例子:

CREATE USER user1 WITH PASSWORD '[email protected]?user1'; 
GRANT CONNECT ON DATABASE database1 TO user1; 
GRANT USAGE ON SCHEMA schema1 TO user1; 

SELECT * FROM grantTablesOfSchema ('user1', 'SELECT, UPDATE, INSERT, DELETE', 'schema1'); 

SELECT * FROM grantSequencesOfSchema ('user1', 'ALL', 'database1', 'schema1'); 

如果,相反,您的PostgreSQL版本是> = 9:

GRANT ALL ON ALL SEQUENCES IN SCHEMA schema1 TO user1; 
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema1 TO user1; 
20
GRANT ALL PRIVILEGES ON     SCHEMA schema_name TO role_name; 
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO role_name; 
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name TO role_name; 
相關問題