我需要授予一个特定用户在PostgreSQL中创建存储过程的权限,而不需要向其他表写入权限。存储过程应该只在一个表中读写。
我已经设置了对该表的读取权限,但在写入权限上却遇到了困难。
GRANT CONNECT ON DATABASE production_database TO user;
GRANT USAGE ON SCHEMA public TO user;
GRANT SELECT ON table TO user;
如果你想在PLPGSQL中写一个存储过程,你需要使用PostgreSQL 11或12。
在PostgreSQL中,没有明确的权限来创建存储过程或函数。
然而你可以尝试
为存储过程创建一个特定的模式。
只允许特定用户使用这个模式。
创建存储过程,将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,表的模式是public。
用这个脚本:
\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)