【问题标题】:How to grant stored procedure creation permission to an user in PostgreSQL如何在 PostgreSQL 中向用户授予存储过程创建权限
【发布时间】:2020-05-14 18:40:08
【问题描述】:

我需要授予特定用户在 PostgreSQL 中创建存储过程的权限,而不需要对其他表的写入权限。存储过程只能在一张表中读写。

我已经设置了对该表的读取权限,但我正在为写入权限而苦苦挣扎。

GRANT CONNECT ON DATABASE production_database TO user;
GRANT USAGE ON SCHEMA public TO user;
GRANT SELECT ON table TO user;

【问题讨论】:

    标签: sql postgresql stored-procedures postgresql-9.5


    【解决方案1】:

    如果您想在 PL/PGSQL 中编写过程,您需要使用 PostgreSQL 11 或 12。

    在 PostgreSQL 中,没有明确的权限来创建过程或函数。

    不过你可以试试:

    • 仅为过程创建特定模式

    • 将此架构的 USAGE 仅授予特定用户

    • 创建以 SECURITY DEFINER 作为表所有者的过程

    例子:

    create user myuser password 'myuser';
    --
    create table public.t(x int);
    --
    create schema myschema;
    --
    create or replace procedure myschema.myproc(param int)
    language plpgsql
    as
    $$
    declare
     v int;
    begin
     insert into public.t values(param);
    end;
    $$
    security definer
    set search_path='';
    --
    grant usage on schema myschema to myuser;
    

    这里的表所有者是超级用户 postgres,表模式是公开的:

    使用此脚本:

    \c postgres myuser
    select * from t;
    call myschema.myproc(1);
    \c postgres postgres
    select * from t;
    

    我明白了:

    You are now connected to database "postgres" as user "myuser".
    select * from t;
    psql:cp.sql:25: ERROR:  permission denied for table t
    call myschema.myproc(1);
    CALL
    You are now connected to database "postgres" as user "postgres".
    select * from t;
     x 
    ---
     1
    (1 row)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多