ysqlsh is the command-line shell for interacting with YugabyteDB using the YSQL API (PostgreSQL-compatible).
Synopsis
Description
ysqlsh is a terminal-based front-end to YugabyteDB. It enables you to:- Execute SQL queries interactively and view results
- Execute queries from files or scripts
- Use meta-commands for database administration
- Automate database operations with scripting
Installation
ysqlsh is installed with YugabyteDB and located in thebin directory. A standalone version is also available through the YugabyteDB clients package.
Default Connection Parameters
When no flags are specified, ysqlsh uses these defaults:- Host:
127.0.0.1(localhost) - Port:
5433 - User:
yugabyte - Database:
yugabyte - Password:
yugabyte(for manually deployed or insecure clusters)
Connection Flags
-h, —host=HOSTNAME
Database server hostname or IP address.-p, —port=PORT
Database server port number (default: 5433).-U, —username=USERNAME
Database user name (default: yugabyte).-d, —dbname=DBNAME
Database name to connect to (default: yugabyte).-W, —password
Force password prompt before connecting.Connection String
You can use PostgreSQL connection strings or URIs:Output Formatting Flags
-A, —no-align
Unaligned table output mode (useful for scripting).-H, —html
HTML table output format.-t, —tuples-only
Print rows only, without column headers and footers.-x, —expanded
Expanded table formatting mode (one column per line).-F, —field-separator=STRING
Field separator for unaligned output (default: pipe|).
-R, —record-separator=STRING
Record separator for unaligned output (default: newline).Query Execution Flags
-c, —command=COMMAND
Execute a single SQL command and exit.-c flags:
-f, —file=FILENAME
Execute commands from a file and exit.-v, —set=, —variable=NAME=VALUE
Set a variable for use in SQL scripts.query.sql:
-1, —single-transaction
Execute file as a single transaction.Behavior Flags
-a, —echo-all
Print all input lines to standard output.-b, —echo-errors
Print failed SQL commands to standard error.-e, —echo-queries
Echo commands sent to the server.-E, —echo-hidden
Display the actual queries executed by meta-commands.\dt to see the underlying SQL.
-L, —log-file=FILENAME
Log session to a file.-n, —no-readline
Disable command-line editing (readline).-o, —output=FILENAME
Redirect query output to a file.-q, —quiet
Quiet mode (suppress welcome message and prompts).-s, —single-step
Single-step mode (confirm each query before execution).-S, —single-line
Single-line mode (newline terminates SQL command).Information Flags
-l, —list
List available databases and exit.-V, —version
Print version and exit.-?, —help[=options]
Show help and exit.Common Usage Examples
Connect to a Database
Execute Queries
Export Data
Run Scripts
Database Administration
Interactive Mode
When started without the-c or -f flags, ysqlsh enters interactive mode:
Interactive Commands
In interactive mode, you can:- Type SQL statements terminated by semicolon (
;) - Use meta-commands starting with backslash (
\) - Use arrow keys for command history
- Use Tab for auto-completion
Environment Variables
ysqlsh respects standard PostgreSQL environment variables:| Variable | Description |
|---|---|
PGHOST | Database server host |
PGPORT | Database server port |
PGDATABASE | Default database name |
PGUSER | Default username |
PGPASSWORD | Default password (not recommended) |
PGPASSFILE | Path to password file (default: ~/.pgpass) |
PGSSLMODE | SSL mode: disable, require, verify-ca, verify-full |
PSQL_HISTORY | Path to history file (default: ~/.psql_history) |
PSQLRC | Path to startup file (default: ~/.psqlrc) |
Password File
Create~/.pgpass with format:
Startup File
Create~/.psqlrc to run commands on startup:
Exit Status
ysqlsh returns:0- Normal termination1- Fatal error (out of memory, file not found)2- Connection error (non-interactive session)3- Script error withON_ERROR_STOPset
Examples with Output
Create and Query Table
Export to CSV
Expanded Output
Security Considerations
-
Avoid PGPASSWORD: Don’t use
PGPASSWORDenvironment variable in production. Use.pgpassfile instead. -
SSL Connections: Use SSL for remote connections:
-
Password File Permissions: Ensure
.pgpasshas mode 0600: -
Command History: Sensitive data may be stored in
~/.psql_history. Clear it if needed:
See Also
- YSQL Meta-Commands - Backslash commands for ysqlsh
- YSQL API - SQL reference for YugabyteDB
- yugabyted - Cluster management tool
- ycqlsh - YCQL shell

