Skip to content

How can I revoke execution of a Postgres function?

所有函数的访问默认都是公开的,这意味着任何角色都可以执行它。要撤销执行,需要两个步骤:

🌐 All functions access is PUBLIC by default, this means that any role can execute it. To revoke execution, there are 2 steps required:

  • 从 PUBLIC 撤销函数执行权限(在本例中是 foo):
1
revoke execute on function foo from public;
  • 从特定角色撤销执行权限(这里是 anon):
1
revoke execute on function foo from anon;

现在 anon 在尝试执行这个函数时应该会报错:

🌐 Now anon should get an error when trying to execute the function:

1
begin;
2
set local role anon;
3
select foo();
4
ERROR: permission denied for function foo