我目前正在使用以下以主用户 (postgres) 身份执行的 SQL 脚本为特定数据库创建角色。
CREATE USER customrole WITH PASSWORD 'mypassword';
-- removed CREATE permissions from the public schema from all roles
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
-- removed all permissions from the database from all users
REVOKE ALL ON DATABASE mydb FROM PUBLIC;
-- added connect permission on the database to role
GRANT CONNECT ON DATABASE mydb TO customrole;
-- added usage permission on public schema to the customrole
GRANT USAGE ON SCHEMA public TO customrole;
-- added create permission on the public schema to the custom role
GRANT CREATE ON SCHEMA public to customrole;
-- added create permission on the database to custom role
GRANT CREATE ON DATABASE mydb to customrole;
-- added 'all permissions' to all existing tables in the public schema to new role
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO customrole;
-- Set default privileges for the postgres user in the public schema
ALTER DEFAULT PRIVILEGES FOR USER masteruser IN SCHEMA public
GRANT ALL PRIVILEGES ON TABLES TO customrole;
现在我想使用角色在现有表上创建索引。例如:
create index my_idx on mytable (mycolumn);
我的发现如下: 以主用户身份执行:
ALTER TABLE "mydb".public.mytable OWNER TO "customrole";
作为自定义角色执行:create index my_idx on mytable (mycolumn);
如果我不执行如上所示的 alter table 命令,我会收到此错误。
错误:SQL 错误 [42501]:错误:必须是表 mytable 的所有者
问题:是否有任何sql命令允许角色(不是主角色)在现有和未来的表中创建索引?
只有表所有者、表所有者的成员(即表所有者允许
SET ROLE
)或超级用户可以在表上创建索引。