LinuxCommandLibrary

sqlx

Run SQL queries with compile-time verification

TLDR

Create the database specified in the DATABASE_URL environment variable

$ sqlx database create
copy

Drop the specified database
$ sqlx database drop [[-D|--database-url]] [database_url]
copy

Create a new pair of up and down migration files with the given description in the "migrations" directory
$ sqlx migrate add -r [migration_description]
copy

Run all pending migrations for the specified database
$ sqlx migrate run [[-D|--database-url]] [database_url]
copy

Revert the latest migration for the specified database
$ sqlx migrate revert [[-D|--database-url]] [database_url]
copy

SYNOPSIS

sqlx [OPTIONS] [COMMAND]

PARAMETERS

--version
    Show application's version.

--help
    Display this help message.

-d, --database
    Database connection URL.

-c, --command
    The SQL query to execute. If omitted, sqlx will expect to read SQL from stdin.

-f, --file
    Execute SQL commands from a file.

--json
    Format output as JSON.

--csv
    Format output as CSV.

DESCRIPTION

sqlx is a powerful command-line tool designed for interacting with various SQL databases. It streamlines the process of executing SQL queries and scripts against databases, making it easy to manage and manipulate data. sqlx supports connections to multiple database types through standard connection strings (PostgreSQL, MySQL, SQLite, MSSQL, etc.). It provides features such as query execution, result formatting (e.g., CSV, JSON), schema introspection, and script execution. sqlx is commonly used by developers, database administrators, and system administrators to automate database tasks, perform ad-hoc queries, and troubleshoot database issues.

<B>USAGE EXAMPLES</B>

1. Execute a query:
sqlx -d postgres://user:password@host:port/database -c "SELECT * FROM users;"

2. Execute a query from a file:
sqlx -d postgres://user:password@host:port/database -f query.sql

3. Format output as JSON:
sqlx -d postgres://user:password@host:port/database -c "SELECT * FROM users;" --json

<B>EXIT CODES</B>

sqlx generally returns 0 for success and non-zero for errors (e.g., invalid query, connection failure).

HISTORY

The history of sqlx depends on the specific implementation being used. It has appeared since many tools fulfill similar functionality over the years. sqlx often evolve as scripting requirements become more demanding and complex.

SEE ALSO

psql(1), mysql(1), sqlite3(1)

Copied to clipboard