我怎样才能创建一个具有有限访问权限、没有插入更新但可以执行重建索引和备份等维护任务的 postgres 角色?

How can I make a postgres role that has limited access, no insert update, but can do maintenance tasks like reindex and backups?

我的 windows 桌面应用程序随我的 postgres 9.3 安装在各处的计算机上。我不想让 Joe Script-kiddy 查看 pgpass.conf,在我的数据库中获取 postgres 密码 运行ning amuck。

我能够使用 this article 做一个备份角色,但我 运行 运气不好,没有使用 reindexdb 和 Vacuum Analyze。

到目前为止,我的解决方案是编写一个应用程序,它将采用加密的 vbscript 文件并使用 ado 进行连接和 运行。

我已经阅读了关于 g运行t 和 role 的 PG 帮助,但我不知道如何执行此操作。如果有任何想法,我将不胜感激。

Neil McGuigan 建议对重建索引和分析有效。不过,我需要在循环中为每个 table 重新编制索引。如果我尝试重新索引数据库 myDb,我会得到 EINDEX DATABASE cannot be executed from a function or multi-command string。我在使用 vacuum [table]

时遇到了同样的错误

我仍在研究 dblink 作为可能的解决方案。 .. 好吧,我不能使用 dblink,因为它需要密码,所以我必须将密码编码到函数中,因为我的备份角色有 select,它可以查看函数的内容。

所以我有一个低特权角色,备份,可以备份、分析和重建索引,但不能清理。在我看来 st运行ge。

您始终可以将命令包装在函数中。 运行 具有 定义者 权限的函数。将执行权限授予其他人。示例:

set role roleThatOwnsTable1;

/*
 SECURITY DEFINER tells Postgres to run the function with the privileges of the role that 
 defined the function, as opposed to the privileges of the role that invoked the function.
*/
create or replace function reindexTable1() returns void SECURITY DEFINER language plpgsql as $$
begin
  reindex table table1;
end $$;

grant execute on function reindexTable1() to someGuy;

set role someGuy;

select reindexTable1();

当然,这确实会向通常不应该拥有它的人开放该功能,所以要小心。