Skip to main content

Run Snowflake queries from GitHub

Running Snowflake queries from GitHub is possible with native Snowflake GitHub integration. To execute a query in Snowflake from Githhub directly, you can follow these steps:

Step 1: Set Up GitHub Repository

If you already have a GitHub repository with your SQL scripts, you can use it. Otherwise, create a new repository for your Snowflake-related scripts.

Organize the Repository:

Store your SQL scripts in a structured format with a .sql file extension For example:

/sql_scripts 
|-- create_tables.sql |-- load_data.sql |-- analytics.sql

Generate a Personal Access Token (PAT):

Navigate to your GitHub profile settings and create a GITHub Personal Access Token. We have documented the process specifically for the purpose of loading and executing SQL in Snowflake.

Step 2: Create a Snowflake Stage

A stage in Snowflake is used to reference external data sources. For GitHub integration, you’ll create an external stage pointing to the GitHub repository.

Create a Named Stage:

The easiest way to create a stage in Snowflake is via the UI.

Navigate to your database

Click Create> Git Repository: Learn how to attach your GitHub stage to Snowflake

Snowflake queries from GitHub

Test the Stage:

  • Use the LIST command to verify the stage is connected properly:
LIST @github_stage;

Step 3: Grant Permissions

Ensure the appropriate roles have access to the stage. Run the following and replace <role_name> with the appropriate Snowflake role.

GRANT USAGE ON STAGE github_stage TO ROLE <role_name>;
GRANT READ ON STAGE github_stage TO ROLE <role_name>;

Step 4: Execute Code from GitHub

Executive your SQL code directly from Github.

EXECUTE IMMEDIATE FROM @sfdc.stage.datatoolspro/branches/main/query_in_repository.sql;

Step 5: Refresh the Stage

To ensure the stage reflects the latest file versions from GitHub, refresh the stage by invalidating its cache:

ALTER STAGE github_stage REFRESH;

This command forces Snowflake to clear its metadata cache and retrieve the latest file versions from GitHub.