cockroach sql command opens an interactive SQL shell for executing queries and managing databases.
Synopsis
Description
Open an SQL shell running against a CockroachDB database. The shell provides:- Interactive query execution
- Statement history and editing
- Multi-line statement support
- Transaction management
- Output formatting options
Connection Flags
Database server host and port to connect to.Can be specified as:
- Hostname:
--host=myserver - Hostname with port:
--host=myserver:26257 - IPv6 address:
--host=[::1]:26257
COCKROACH_HOSTDatabase user for authentication.Environment variable:
COCKROACH_USERDatabase to connect to. Can optionally include virtual cluster:Environment variable:
COCKROACH_DATABASEConnection URL (alternative to individual flags).Format: Environment variable:
postgresql://[user[:password]@]host[:port]/[database][?options]COCKROACH_URLSecurity Flags
Path to directory containing client certificates.Required files:
ca.crt- CA certificateclient.<user>.crt- Client certificateclient.<user>.key- Client private key
Connect without TLS encryption.
Query Execution Flags
Execute SQL statement(s) and exit. Multiple statements separated by semicolons.
Execute SQL statements from a file.
Repeat the SQL statement(s) at the specified interval.
Output Formatting Flags
Output format for query results.Options:
table- ASCII table (default for interactive)tsv- Tab-separated valuescsv- Comma-separated valuessql- SQL statementsraw- Raw values without formattingrecords- One column per linehtml- HTML table
Set a client-side option. Can be used multiple times.Common options:
display_format- Output formaterrexit- Exit on errorecho- Echo statements before execution
Interactive Shell Commands
When running in interactive mode, these special commands are available:| Command | Description |
|---|---|
\q | Quit the shell |
\! | Execute shell command |
\set | Set client option |
\unset | Unset client option |
\show | Show current client options |
\? | Show help on backslash commands |
\d [table] | Describe table or list all tables |
\dt | List tables |
\dT | List types |
\l | List databases |
\c [database] | Connect to database |
\echo | Print arguments to stdout |
Examples
Connect to Local Cluster
Connect to Secure Cluster
Execute Single Statement
Execute Multiple Statements
Run SQL File
Connect with Full URL
Export Data as CSV
Watch Query Results
Interactive Session Example
SQL Shell Configuration
Client-Side Options
Set using\set in interactive mode or --set flag:
Output format (table, csv, tsv, html, records, sql, raw)
Exit on first error when executing from file or
-eEcho SQL statements before executing
Display query execution times
Example Configuration
Tips and Best Practices
Interactive Shell Tips:
- Use
\?to see all available backslash commands - Press
Ctrl+Cto cancel current statement input - Use
Ctrl+Rfor reverse search through command history - Multi-line statements automatically continue until semicolon
- Use
\d tablenamefor quick table schema inspection
Troubleshooting
Connection Refused
- Verify the server is running:
cockroach node status - Check hostname and port are correct
- Ensure firewall allows connections on the SQL port
SSL/Certificate Errors
- Verify certificates exist in
--certs-dir - Check certificate permissions (keys should be 600)
- Ensure certificates haven’t expired
- Verify CA certificate matches server’s CA
Authentication Failures
- Check username is correct
- Verify user exists: run as root and query
system.users - Ensure user has appropriate grants