Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation of Table using DBT

Tags:

dbt

Can we create a new table in DBT? Can we copy the table structure which is present in the dev environment in the database to another environment using DBT?

like image 258
annie679 Avatar asked Oct 15 '25 15:10

annie679


1 Answers

Yes. However, Dbt needs a "reason" to create tables, for example, to materialize the data produced by one of its models. DBT cannot create table just for the creation's sake.

Well, strictly speaking, you can do this by putting CREATE TABLE... in a pre-hook or post-hook section, but I suppose this is not what you want since dbt makes no difference here at all.

You can define your existed table in sources where you can set database, schema and table name different from the target storage space where dbt writes data. And then, define a model something like:

{{ materialized="table" }}
select * 
from {{ source('your_source', 'existed_table_name') }}
limit 1 /* add "limit 1" if you only want the structure  */ 

Put necessary connection credentials in the profiles.yml, and build the model. Dbt will copy one row from source table into model table, before that model table creation is done for free.

like image 161
Bing-hsu Gao Avatar answered Oct 17 '25 22:10

Bing-hsu Gao