Introduction

SnowSQL is the command line client for Snowflake. It allows you to execute SQL queries and perform all DDL and DML operations. It’s an easy way to access snowflake right from your command line and has all the same capabilities as the Snowflake UI.

Step 1 – Download and install SnowSQL CLI

  • Login into Snowflake and click on help in the top right corner
  • Click on Downloads -> Snowflake Repository

This will lead you to a web index page.

  • Click on bootstrap -> 1.2 (or newest version) -> Pick your OS (Darwin is Mac) -> Download the latest version
  • Run the installer.

Step 2 – Running SnowSQL CLI

  • Check SnowSQL is installed properly

Run: snowsql -v

Output: Version: 1.2.5 (or latest)

Good! Now that snowsql has been installed, let’s set up our environment to work.

  • Login to your account

snowsql -a account_name -u username

Account name can be found in the first part of your url when logged into snowflake (everything before snowflakecoputing.com, for instance sample_username.sample_region.azure)

  • Setup the database context

// create a warehouse

CREATE WAREHOUSE yourname_WH AUTO_SUSPEND = 60 AUTO_RESUME=TRUE;

USE WAREHOUSE yourname_WH;

// select your desired database

USE DATABASE SNOWFLAKE_SAMPLE_DATA;

// select the data schema

USE SCHEMA TPCDS_SF100TCL;

(Note: Lukes-MacBook-Pro and lmunro are specific to my console. Yours will be different unless you somehow stole my laptop in which case please give it back.)

Awesome! Now we’re ready to perform whatever data analytics you desire.

However, it can be quite tedious to type in your account, username, password, warehouse, DB, and schema every time you login. You can edit the snowSQL config file to perform these automatically.

Step 3 – Edit Config File

  • Locate hidden snowsql folder
    • Linux/Mac OS: ~/.snowsql/
    • Windows: your-user-folder.snowsql
  • Open the file named config and add the following

[connections.configuration-name]

accountname = your_account_name

username = your_username

password = your_password

dbname = SNOWFLAKE_SAMPLE_DATA

warehousename = yourname_WH

schemaname = TPCDS_SF100TCL

  • Save and exit
  • Connect using the following command

snowsql -c configuration-name

Step 4 – Modify Display Prompt

SnowSQl prompt automatically displays the current user, warehouse, database and schema. The tokens can be seen in the image above. This prompt can be a bit lengthy but you can edit the prompt with the following command:

!set prompt_format=>>

To auto change the prompt format, add the following to the configuration file.

[options]

# auto_completion gives you possible existing options, very helpful!

auto_completion = True

prompt_format=>>

Conclusion

SnowSQL CLI is a quick way to plug into Snowflake directly from the terminal. It’s preferable to the UI if you already have a grasp of terminal operations and don’t require the UI to navigate around.

The config file in the SnowSQL folder is where you can set configuration and options for the CLI. You can preset login credentials and database settings by adding a [connections.***] block and specify options by adding to the [options] block. For more information check out the public documentation.

Q&A

What does the -c mean?

Whenever you run a program in the terminal you can specify arguments with a dash (-). The -c parameter tells the program snowsql to look in ~/.snowsql/config for a connection named lmunro_config. It then uses those credentials and other configurations to quickly log you in and set up your environment. Note: -c is an abbreviation. You can also use –connection.

Are there any other parameters that I should know about?

Yes! There are a bunch of parameters which can make your life easier. In fact, you can login and set up your environment all in one line, like this:

snowsql -a **.east-us-2.azure -u lmunro -d SNOWFLAKE_SAMPLE_DATA -s TPCDS_SF100TCL -w LUCASMUNRO_WH

Don’t worry if that’s a bit overwhelming. You can (and should) use the config file so you don’t need to type it all out. If you’re interested in using these parameters or want more information check out the docs.