暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

如何为PostgreSQL中的角色设置默认参数

飞象数据 2019-08-02
329

你可能了解在PostGreSQL的会话级别使用set
命令来设置各种不同的参数:

postgres=# \h setCommand:     SETDescription: change a run-time parameterSyntax:SET [ SESSION | LOCAL ] configuration_parameter { TO | = } { value | 'value' | DEFAULT }SET [ SESSION | LOCAL ] TIME ZONE { timezone | LOCAL | DEFAULT }
复制

当你需要特别设置参数时可以在会话运行时调整参数,这是一种方便的动态配置方法。如果我们可以为一个角色或用户设置一组默认参数,这不是更好吗?如果有一个用户需要专门设置work_mem
参数,另一个用户需要专门设置search_path
参数时。你可以在服务器端进行设置而不是每当会话连接后进行设置。

创建用户a
和用户b

postgres=# create user a login password 'a';CREATE ROLEpostgres=# create user b login password 'b';CREATE ROLEpostgres=# \du                                   List of roles Role name |                         Attributes                         | Member of-----------+------------------------------------------------------------+----------- a         |                                                            | {} b         |                                                            | {} postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
复制

每当用户a
创建一个新的连接时需要专门设置work_mem
参数,每当用户b
创建一个新的连接时需要专门设置search_path
参数。

postgres=# alter user a set work_mem = '1MB';ALTER ROLEpostgres=# alter user b set search_path='b';ALTER ROLEpostgres=#
复制

当用户a
连接时:

postgres=# \c postgres aYou are now connected to database "postgres" as user "a".postgres=> show work_mem; work_mem---------- 1MB(1 row)
复制

当用户b
连接时:

postgres=> \c postgres bYou are now connected to database "postgres" as user "b".postgres=> show search_path ; search_path------------- b(1 row)
复制

注意的是这并没有阻止用户修改:

postgres=> select current_user; current_user-------------- b(1 row) postgres=> set search_path=c;SETpostgres=> show search_path ; search_path------------- c(1 row)
复制

这里指更多的设置是与主服务器默认配置不同的设置。如何知道哪些用户配置了哪些设置呢?很简单呐,使用pg_roles
查看:

postgres=> select rolname,rolconfig from pg_roles where rolname in ('a','b'); rolname |    rolconfig    ---------+----------------- a       | {work_mem=1MB} b       | {search_path=b}(2 rows)
复制

本文翻译自:https://blog.dbi-services.com/setting-up-default-parameters-for-roles-in-postgresql/

文章转载自飞象数据,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论