LinuxCommandLibrary

snowsql

Connect and query Snowflake databases

TLDR

Connect to a specific instance at (password can be provided in prompt or configuration file)

$ snowsql --accountname [account] --username [username] --dbname [database] --schemaname [schema]
copy

Connect to an instance specified by a specific configuration file (defaults to ~/.snowsql/config)
$ snowsql --config [path/to/configuration_file]
copy

Connect to the default instance using a token for multi-factor authentication
$ snowsql --mfa-passcode [token]
copy

Execute a single SQL query or SnowSQL command on the default connection (useful in shell scripts)
$ snowsql --query '[query]'
copy

Execute commands from a specific file on the default connection
$ snowsql --filename [path/to/file.sql]
copy

SYNOPSIS

snowsql [options] [arguments]

Examples:

snowsql -a -u
snowsql -f -o exit_on_error=true
snowsql -q "SELECT CURRENT_VERSION();"

PARAMETERS

-a, --account
    Specifies the Snowflake account identifier to connect to.

-u, --username
    The Snowflake username for authentication.

-w, --warehouse
    Sets the default virtual warehouse for the session.

-d, --database
    Sets the default database for the session.

-s, --schema
    Sets the default schema within the database for the session.

-r, --role
    Specifies the role to use for the session.

-P, --password
    Provides the password directly (not recommended for security). Use --prompt-for-password instead.

--prompt-for-password
    Prompts the user for their password securely.

-f, --filename
    Executes SQL commands from the specified file.

-q, --query
    Executes a single SQL query and then exits.

-o, --option
    Sets a SnowSQL configuration option (e.g., exit_on_error=true).

-D, --define
    Defines a variable that can be used within SQL scripts.

-v, --version
    Displays the SnowSQL version information and exits.

-h, --help
    Shows the help message and exits.

DESCRIPTION

SnowSQL is the powerful command-line client provided by Snowflake, enabling users to connect to and interact with their Snowflake accounts directly from a terminal. It serves as an essential tool for executing SQL queries, loading and unloading data, and performing various administrative tasks. Built on Python, SnowSQL supports both interactive and non-interactive modes, making it versatile for scripting, automation, and ad-hoc query execution. It provides features like command history, tab completion, and customizable options through a configuration file, enhancing productivity for data professionals. Users can manage databases, schemas, tables, warehouses, and roles, facilitating comprehensive control over their Snowflake environment.

CAVEATS

Security: Avoid passing sensitive credentials like passwords directly on the command line, as they might be visible in process lists. Use environment variables (e.g., SNOWSQL_PWD), a configuration file (~/.snowsql/config), or the --prompt-for-password option for better security.

Performance: For extremely large data loading or unloading scenarios, consider using Snowflake's bulk loading features (e.g., PUT/GET commands or Snowpipe) which might offer better performance than simple SQL inserts or selects through SnowSQL.

Dependencies: SnowSQL requires a compatible Python environment to run. Ensure Python and necessary libraries are correctly installed.

CONFIGURATION FILE

SnowSQL can be configured via a file located at ~/.snowsql/config. This file allows users to store default connection parameters, credentials (encrypted), and various SnowSQL options, streamlining the connection process and enhancing security by avoiding command-line arguments for sensitive information.

INTERACTIVE VS. NON-INTERACTIVE MODE

SnowSQL operates in two primary modes. In interactive mode, it provides a shell for executing SQL queries sequentially, with features like command history and tab completion. In non-interactive mode (e.g., using -q or -f), it executes specified queries or scripts and exits, making it suitable for automation and scripting within CI/CD pipelines or scheduled jobs.

HISTORY

SnowSQL has been a core component of the Snowflake ecosystem since its early days, designed to provide a robust command-line interface for interacting with the Snowflake Data Cloud. While Snowflake was founded in 2012 and launched its service in 2014, SnowSQL has continuously evolved, incorporating new features and optimizations in parallel with the growth and expansion of the Snowflake platform itself. It maintains backward compatibility while adapting to new authentication methods, query capabilities, and data management features offered by Snowflake.

SEE ALSO

psql(1), mysql(1), sqlcmd(1), python(1)

Copied to clipboard