Snowflake SnowSQL Command Line
Snowsight suits exploring and learning. Automated scripts, file uploads, and scheduled jobs need a tool that runs without a browser. SnowSQL is Snowflake's command-line client. It runs SQL, uploads files with PUT, and fits neatly into shell scripts and scheduled tasks.
Where SnowSQL Fits
Think of Snowsight as a car with a dashboard and touchscreen. SnowSQL is the engine's service port: no visuals, direct access, and ideal for mechanics who repeat the same job every day. Data engineers rely on it to push local files into stages and run deployment scripts.
SNOWSQL IN THE WORKFLOW
=======================
+-----------+ +-----------+ +--------------------+
| Terminal | | SnowSQL | | SNOWFLAKE ACCOUNT |
| or script |---->| client |---->| stages, tables, |
| | | | | warehouses |
+-----------+ +-----------+ +--------------------+
^ |
| v
Local files Output as table, CSV, or JSONInstalling and Connecting
Download the installer for Windows, macOS, or Linux from the Snowflake website. Open a terminal and check the version.
snowsql -v
# Connect with account identifier and user name
snowsql -a myorg-myaccount -u analyst_userSnowSQL asks for the password at the prompt. Never type passwords into scripts. The SNOWSQL_PWD environment variable, key pair authentication, or a saved connection supplies credentials safely.
Saved Connections
The configuration file stores connection details under named profiles. The default path is ~/.snowsql/config on macOS and Linux, and %USERPROFILE%\.snowsql\config on Windows.
[connections.dev]
accountname = myorg-myaccount
username = analyst_user
rolename = ANALYST_ROLE
warehousename = ANALYST_WH
dbname = SALES_DB
schemaname = PUBLIC
authenticator = externalbrowser
snowsql -c devThe externalbrowser authenticator opens a browser window for single sign-on, so no password sits in the file.
Running Queries and Scripts
# One query from the command line
snowsql -c dev -q "SELECT COUNT(*) FROM orders"
# Run a whole file
snowsql -c dev -f deploy.sql
# Export results as CSV without extra decoration
snowsql -c dev -q "SELECT * FROM orders" \
-o output_format=csv -o header=true \
-o timing=false -o friendly=false > orders.csvUploading and Downloading Files
The PUT command works only in SnowSQL and other drivers. Worksheets in Snowsight cannot reach files on your computer. PUT compresses files with gzip by default before upload.
-- Upload one file to a named internal stage
PUT file:///home/user/data/orders.csv @sales_stage AUTO_COMPRESS = TRUE;
-- Upload every CSV in a folder
PUT file:///home/user/data/*.csv @sales_stage;
-- Check the stage
LIST @sales_stage;
-- Load into a table
COPY INTO orders FROM @sales_stage FILE_FORMAT = (TYPE = CSV SKIP_HEADER = 1);
-- Download files back to the local machine
GET @sales_stage/export/ file:///home/user/downloads/;Useful Meta Commands
COMMAND PURPOSE
--------------------------- -----------------------------------------
!help List all meta commands
!set variable_substitution=true Turn on variable replacement
!define env=DEV Create a variable
!source script.sql Run another file
!spool results.txt Save output to a file
!spool off Stop saving output
!exit or !quit Leave SnowSQLVariables in Scripts
snowsql -c dev -D env=TEST -f deploy.sql
-- Inside deploy.sql
USE DATABASE SALES_&env;
CREATE TABLE IF NOT EXISTS &env._audit (event_ts TIMESTAMP, note STRING);The same script now deploys to TEST, DEV, or PROD by changing one command-line value.
A Simple Nightly Script
#!/bin/bash
snowsql -c prod -q "PUT file:///data/exports/daily_*.csv @sales_stage"
snowsql -c prod -f /scripts/load_daily.sql
if [ $? -ne 0 ]; then
echo "Load failed" | mail -s "Snowflake load error" ops@example.com
fiSchedule the script with cron on Linux or Task Scheduler on Windows.
Newer Tool: Snowflake CLI
Snowflake offers the Snowflake CLI, called snow, for developer projects such as apps, Snowpark code, and Streamlit. SnowSQL remains popular for plain SQL scripts and file transfers.
A Practical Scenario
A logistics company receives shipment files from partners on a server every evening. A small shell script uploads the files with PUT, runs a SQL file that loads and validates them, and sends an email when a step fails. The operations team never opens a browser for this task. A named connection stores the account details, and key pair authentication keeps passwords out of the script.
Frequently Asked Questions
Can I run SnowSQL on a server without a screen?
SnowSQL runs in any terminal session, including remote servers, containers, and scheduled jobs.
How do I stop SnowSQL from showing timing lines in exported files?
Set the timing and friendly options to false, and choose the output format that suits the downstream tool.
Handling Errors in Scripts
SnowSQL returns a non-zero exit code when a statement fails. Shell scripts read that code and decide whether to stop, retry, or send a warning. Add the exit-on-error option to a script so one failed statement ends the run and protects later steps from incomplete data.
Security Habits
Restrict file permissions on the configuration file so only your user can read it. Prefer single sign-on or key pair authentication for any shared server, and rotate keys on a regular schedule. Log script output to a protected folder because query text sometimes contains business details.
Key Points
- SnowSQL runs SQL and file transfers from a terminal or script.
- Named connections in the config file avoid retyping details.
- PUT and GET move files between a local machine and stages.
- Variables let one script deploy to several environments.
- Output options export clean CSV or JSON for other tools.
- Credentials belong in environment variables, keys, or single sign-on, never in scripts.
