YDB CLI - Getting started

Prerequisites

To run commands via the CLI, you will need database connection settings you can get when creating a database:

You may also need a token or login/password if the database requires authentication. To execute the below scenario, you need to select an option for saving them in an environment variable.

Installing the CLI

Install the YDB CLI as described in Installing the YDB CLI.

To check that the YDB CLI has been installed, run it with --help:

  1. ydb --help

YDB command line interface (CLI) - 图1

The response includes a welcome message, a brief description of the syntax, and a list of available commands:

  1. YDB client
  2. Usage: ydb [options...] <subcommand>
  3. Subcommands:
  4. ydb
  5. ├─ config Manage YDB CLI configuration
  6. └─ profile Manage configuration profiles
  7. ├─ activate Activate specified configuration profile (aliases: set)
  8. ...

YDB command line interface (CLI) - 图2

All the features of the YDB CLI built-in help are described in Built-in help of the YDB CLI reference.

Check the connection

To test connection, you can use the command for listing objects in the database, scheme ls:

  1. ydb -e <endpoint> -d <database> scheme ls

YDB command line interface (CLI) - 图3

If the command is successful, a list of objects in the database is shown in response. If you haven’t created anything in the database yet, the output will only contain the .sys and .sys_health system directories with diagnostic views of YDB.

For example, if:

  • Endpoint: grpc://ydb.example.com:2136.
  • Database name: /john/db1.
  • The database doesn’t require authentication, or the proper environment variable has been set, as described here.
  • A database has just been created and contains no objects.

the command will look like this:

  1. ydb -e grpc://ydb.example.com:2136 -d /john/db1 scheme ls

YDB command line interface (CLI) - 图4

The command execution result on the created empty database:

  1. .sys_health .sys

YDB command line interface (CLI) - 图5

Connecting to a local database

If you deployed a local YDB using a scenario for self-hosted deployment in Docker with the suggested configuration, you can check a YDB connection using the command:

  1. ydb -e grpc://localhost:2136 -d /local scheme ls

YDB command line interface (CLI) - 图6

Creating a connection profile

To avoid specifying connection parameters every time you call the YDB CLI, use the profile. Creating the profile described below will also let you copy subsequent commands through the clipboard without editing them regardless of which database you’re using to complete the “Getting started” scenario.

Create the profile db1 using the following command:

  1. ydb config profile create db1

YDB command line interface (CLI) - 图7

You will be interactively prompted for connection parameters to be linked with the profile. Use for them the values verified in the previous step.

Check that the profile is OK with the scheme ls command:

  1. ydb -p db1 scheme ls

YDB command line interface (CLI) - 图8

Executing an YQL script

The YDB CLI yql command lets you execute any command (both DDL and DML) in YQL, an SQL dialect supported by YDB:

  1. ydb -p <profile_name> yql -s <yql_request>

YDB command line interface (CLI) - 图9

For example:

  • Creating a table:

    1. ydb -p db1 yql -s "create table t1( id uint64, primary key(id))"

    YDB command line interface (CLI) - 图10

  • Adding a record:

    1. ydb -p db1 yql -s "insert into t1(id) values (1)"

    YDB command line interface (CLI) - 图11

  • Data selects:

    1. ydb -p db1 yql -s "select * from t1"

    YDB command line interface (CLI) - 图12

If you get the Profile db1 does not exist error, that means you neglected to create a profile in the previous step.

Specialized CLI commands

Executing commands via ydb yql is a nice and easy way to get started. However, the YQL interface supports a part of the function set provided by the YDB API, and has to sacrifice efficiency for universality.

The YDB CLI supports individual commands with complete sets of options for any existing YDB API. For a full list of commands, see the YDB CLI reference.

Next step

Go to YQL - Getting started to proceed with the ‘Getting started’ scenario.