You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dbt-postgres模型生成表时schema重复添加public前缀问题

dbt-postgres 表 Schema 重复问题解决

问题现象

本地搭建dbt-postgres测试环境时,创建的表出现Schema重复:

  • 期望生成:public.test
  • 实际生成:public_public.test
    即使在模型中指定其他Schema,也会生成带public_前缀的新Schema。

相关配置文件

模型文件 test.sql

{{ config(materialized='table', schema='public') }}

select a from table x

dbt_project.yml(原标注文件名dbt_profile.yml应为dbt_project.yml)

name: 'abc'
version: '0.1'
config-version: 2
profile: 'abc'
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target" 
clean-targets:       
  - "target"
  - "dbt_packages"
  - "dbt_modules"
  - "logs"

models:
  abc:
      materialized: table

profiles.yml

abc:
  outputs:
    dev:
      type: postgres
      threads: 1
      host: "localhost"
      port: 5432
      user: "admin"
      pass: "admin"
      dbname: database
      schema: "public"
  target: dev

问题原因与解决方法

核心原因

dbt默认会将profiles.yml中指定的目标Schema与模型配置里的Schema进行拼接。你同时在两处指定了public,就会出现public_public的重复结果。

解决方案

场景1:所有模型默认使用public Schema

  • 直接删除模型文件中的schema='public'配置,dbt会自动使用profiles.yml中指定的public Schema,生成的表即为public.test。
  • 若要统一配置所有模型的Schema,也可在dbt_project.yml的models块中添加schema: public:
    models:
      abc:
          materialized: table
          schema: public
    

场景2:部分模型需指定不同Schema

  • 移除profiles.yml中dev输出里的schema: "public"配置(或改为你的默认基础Schema,比如analytics),之后在需要指定Schema的模型中单独配置schema='public',此时dbt不会拼接前缀,直接使用模型指定的Schema。

内容的提问来源于stack exchange,提问作者Dametime

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:00:41