Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt

Use this file to discover all available pages before exploring further.

The sqlx CLI is the companion tool to the SQLx Rust library. It lets you create and drop databases, author and apply SQL migrations, and generate the offline query metadata that allows your project to compile without a live database connection. Every subcommand requires a database URL, either from the DATABASE_URL environment variable, a .env file in the current directory, or the --database-url flag.

Installation

Install sqlx-cli with Cargo. The default installation supports every database backend that SQLx supports and uses the platform’s native TLS library.
cargo install sqlx-cli
Add mysql-rsa only if you connect to MySQL or MariaDB without TLS and the server requires RSA-based authentication (caching_sha2_password or sha256_password). When TLS is in use, this feature is not needed.
cargo install sqlx-cli --features mysql-rsa

Providing a database URL

All commands need to know which database to connect to. You can supply this in three ways:
1

Environment variable

Export DATABASE_URL before running any sqlx command:
export DATABASE_URL=postgres://postgres@localhost/my_database
2

.env file

Create a .env file in the directory where you run sqlx. The CLI loads it automatically:
.env
DATABASE_URL=postgres://postgres@localhost/my_database
3

--database-url flag

Pass the URL directly on the command line to override either of the above:
sqlx database create --database-url postgres://postgres@localhost/my_database

Connection URL formats

DatabaseExample URL
PostgreSQLpostgres://user:password@localhost/dbname
MySQL / MariaDBmysql://user:password@localhost/dbname
SQLitesqlite:./path/to/database.db

Commands overview

The CLI is organized into three command groups:
CommandDescription
sqlx databaseCreate, drop, reset, or set up a database
sqlx migrateAdd migration files, apply pending migrations, revert, and check status
cargo sqlx prepareGenerate and validate offline query metadata for query! macros
Run sqlx <command> --help for a full list of flags for any subcommand.

Next steps

sqlx database

Create and drop databases.

sqlx migrate

Author, apply, revert, and inspect migrations.

cargo sqlx prepare

Generate offline query metadata for CI builds.

Build docs developers (and LLMs) love