Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将架构添加到postgresql中的路径_Sql_Database_Database Design_Postgresql_Schema - Fatal编程技术网

将架构添加到postgresql中的路径

将架构添加到postgresql中的路径,sql,database,database-design,postgresql,schema,Sql,Database,Database Design,Postgresql,Schema,我正在将应用程序从公共模式中的所有应用程序转移到每个应用程序都有自己的模式。对于每个应用程序,我都有一个小脚本,它将创建模式,然后创建表、函数等。。。到那个模式。是否自动将新创建的架构添加到搜索路径?目前,我看到的唯一方法是找到用户当前路径SHOW search\u path然后向其添加新模式将搜索路径设置为xxx,yyy,zzz 我想用某种方式说,将schema zzz附加到用户的搜索路径。这可能吗?使用如下功能: SELECT set_config( 'search_path',

我正在将应用程序从公共模式中的所有应用程序转移到每个应用程序都有自己的模式。对于每个应用程序,我都有一个小脚本,它将创建模式,然后创建表、函数等。。。到那个模式。是否自动将新创建的架构添加到搜索路径?目前,我看到的唯一方法是找到用户当前路径
SHOW search\u path
然后向其添加新模式
将搜索路径设置为xxx,yyy,zzz

我想用某种方式说,将schema zzz附加到用户的搜索路径。这可能吗?

使用如下功能:

SELECT set_config(
    'search_path',
    current_setting('search_path') || ',zzz',
    false
) WHERE current_setting('search_path') !~ '(^|,)zzz(,|$)';

在理论答案的基础上,以下是如何将模式永久性地前置到另一个用户的搜索路径。用于设置只读用户和在不同的.sql文件中拆分各种架构的设置

create or replace function prepend_search_path(
    role_name text, schema_name text
) returns void as $$
declare
    current_search_path text;
begin
    -- First, we get the current search_path for that user
    select  replace(sc.configval,'search_path=','')
    from    pg_db_role_setting rs
    left 
    join    pg_roles r
    on      r.oid = rs.setrole,
    lateral unnest(rs.setconfig) as sc(configval)
    where   sc.configval like 'search_path=%'
    and r.rolname = role_name
    into current_search_path;

    -- It is possible that a new user is not in pg_roles. To fix this,
    -- we find the default search_path values.
    if not found then
        select boot_val
        from pg_settings
        where name='search_path'
        into current_search_path;
    end if;

    -- Prepend the schema_name to search_path
    if current_search_path !~ ('(^|, )' || schema_name || '(,|$)') then
        current_search_path := schema_name || ', ' || current_search_path;
    end if;


    -- Make the changes
    execute format('alter role %I set search_path = %s', role_name, current_search_path);
end
$$ language plpgsql;

为什么是预赛?取决于您的用例。在我看来,为每个存储过程设置模式是很有用的。这意味着,如果更改存储过程,只需在单独的架构中定义它,并覆盖使用它的用户的搜索路径。

我相信这只会为当前会话设置搜索路径,而不会在该会话之后保持不变。我在使用该正则表达式时遇到问题,因为我的路径在逗号后有一个空格。我使用的是
”(^ |,\s?)zzz(,|$)”