Connect to Trino via CLI
Overview
The Trino service has a command line interface (CLI) that allows submitting SQL statements from a remote terminal (relative to the Trino cluster). It is implemented by a self-executing JAR file which can be launched in any OS provided there is a Java runtime environment (JRE) installed.
Setup
Remote machine
Before you can use the Trino CLI, make sure that JRE version 8 or higher is installed and the Trino Cooridinator component is network accessible.
To use the Trino CLI, you need to download the JAR executable. The link to the most recent file is available at the Trino documentation website. If you use Linux OS, rename the file to trino and make it executable using the chmod +x
command. If you use Windows OS, run the Windows CLI as administrator before you launch the JAR file in it.
ADH
Make sure that Trino catalogs are set up for the data sources you wish to access via CLI as described in the Catalog management in Trino article. If you need to add a new catalog, restart the Trino service after doing so.
Usage
Start
To launch the Trino CLI, run the terminal console in your OS and use the following command syntax:
Make sure you use the network address of the Trino Coordinator component. If the connection is successful, a Trino CLI prompt will appear. To view the list of supported commands, execute the help
command. Example:
help
Supported commands: QUIT EXIT CLEAR EXPLAIN [ ( option [, ...] ) ] <query> options: FORMAT { TEXT | GRAPHVIZ | JSON } TYPE { LOGICAL | DISTRIBUTED | VALIDATE | IO } DESCRIBE <table> SHOW COLUMNS FROM <table> SHOW FUNCTIONS SHOW CATALOGS [LIKE <pattern>] SHOW SCHEMAS [FROM <catalog>] [LIKE <pattern>] SHOW TABLES [FROM <schema>] [LIKE <pattern>] USE [<catalog>.]<schema>
To clear the terminal screen, use the clear
command. To exit the Trino CLI and go back to the OS CLI, use the exit
or quit
commands.
You can also use various options when starting the Trino CLI. List of those options is provided on the Command line interface page of the Trino documentation.
Statements execution
When the Trino CLI prompt is active, you can run SQL statements. The CLI will return the results of statements processing and their statistics. Example:
select * from "hive-adh"."default"."solar_objects";
name | mass | diameter ---------+------------+---------- Sun | 1989100000 | 1392000 Mercury | 330 | 4879 Venus | 4867 | 12104 Earth | 5972 | 12742 Mars | 642 | 6780 Jupiter | 1898187 | 139822 Saturn | 568317 | 116464 Uranus | 86813 | 50724 Neptune | 102413 | 49244 (9 rows) Query 20250115_092203_00003_7thb6, FINISHED, 1 node Splits: 1 total, 1 done (100,00%) 1,51 [9 rows, 172B] [5 rows/s, 114B/s]
In this example hive-adh
is the name of the catalog, default
is the DB name, and solar_objects
is the table name.
You can specify the default catalog and DB when launching the Trino CLI. To do that, enter their names consecutively through slashes immediately after the network address and port. Example:
$ ./trino http://cooridinator.trino.example:18188/hive-adh/default
In this case, the command prompt will contain the DB name. This is equivalent to using the USE
statement, which allows you to specify only the tables names when composing statements against them. Example:
select * from "solar_objects";
name | mass | diameter ---------+------------+---------- Pluto | 3897 | 473947 Sun | 1989100000 | 1392000 Mercury | 330 | 4879 Venus | 4867 | 12104 Earth | 5972 | 12742 Mars | 642 | 6780 Jupiter | 1898187 | 139822 Saturn | 568317 | 116464 Uranus | 86813 | 50724 Neptune | 102413 | 49244 (10 rows) Query 20250116_072842_00002_rpy4j, FINISHED, 2 nodes Splits: 2 total, 2 done (100,00%) 2,11 [10 rows, 200B] [4 rows/s, 95B/s]
TLS/HTTPS
Trino CLI supports secure connections using TLS. If TLS and certificate usage is configured in your network, then just use the HTTPS URL for the Trino Coordinator component network address when launching the Trino CLI.
Refer to the Command line interface page of the Trino documentation for available options of TLS usage in Trino CLI.
Configuration files
Sometimes you may need to use a specific set of options in a single command in Trino CLI on a repetitive basis. Instead of doing this manually every time, you can assign custom defaults to any number of options available in Trino CLI and put them in a file, then just invoke that file when executing the command. Every time a command is executed, Trino CLI looks for configuration files and picks the first one found. The priority of the search is as follows:
-
File specified by the
TRINO_CONFIG
environment variable. -
The .trino_config file located in the current user home directory.
-
The $XDG_CONFIG_HOME/trino/config file.
For example, you can fill the .trino_config file up with basic defaults, and create a number of files with special (alternating) defaults. In this case, you can define the TRINO_CONFIG
variable as the path to whichever file you need at the moment, and then unset it to go back to the .trino_config file defaults.