The problem I’m having
I’m using dbt-core to build a view in Snowflake. The view is in a separate schema than the data table.
source table: MY_DATABASE.MY_SCHEMA.MY_TABLE
view: MY_DATABASE.STAGING_SCHEMA.RAW_VIEW
The role I have assigned in Snowflake grants me permissions to create a view in STAGING_SCHEMA.
When I run the model file to create the view, I get this error:
Database Error
003001 (42501): SQL access control error:
Insufficient privileges to operate on database 'MY_DATABASE'
If I run the compiled code in Snowflake with the same role, I can create the view with no issues. More details below.
Details of environment
I’m running this in a repo in VS Code with
dbt-core==1.8.4
dbt-snowflake==1.8.3
I can connect to Snowflake with no errors when I run dbt debug --connection
.
Project structure and files
The my_project
folder structure has a single sql file in models/raw_views
called raw_view.sql
.
My profiles.yml file has:
my_project:
outputs:
dev:
account: ****************
database: MY_DATABASE
password: ***************
role: MY_ROLE
schema: STAGING_SCHEMA
threads: 4
type: snowflake
user: MYUSERNAME
warehouse: MY_WAREHOUSE
target: dev
My dbt_project.yml file looks like:
name: 'my_project'
version: '1.0.0'
profile: 'my_project'
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]
target-path: "target"
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"
models:
my_project:
+materialized: 'view'
+database: 'MY_DATABASE'
+schema: 'STAGING_SCHEMA'
In models/raw_views
I have a schema.yml file:
version: 2
models:
- name: raw_view
description: "view of source table"
sources:
- name: source
database: MY_DATABASE
schema: MY_SCHEMA
tables:
- name: MY_TABLE
description: "my source table"
and the model file raw_view.sql
has:
CREATE OR REPLACE VIEW MY_DATABASE.STAGING_SCHEMA.raw_view AS (
select * from {{ source('source', 'MY_TABLE') }}
)
This is hard coded so I can grab the compiled code and run it to create the view. Otherwise it was just a select * from my_table.
In Snowflake, MY_ROLE has these privileges:
MY_DATABASE: usage
MY_SCHEMA: usage
STAGING_SCHEMA: usage, create view
Questions
Since the create view code works in Snowflake, I am at a loss to why I need elevated privileges on the database level. My admin does not want to grant them unless absolutely needed. Since I have privileges to create views on the STAGING_SCHEMA, is there something I am doing wrong on dbt? Or do I actually need elevated privileges?