TheDocumentation 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.
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
Installsqlx-cli with Cargo. The default installation supports every database backend that SQLx supports and uses the platform’s native TLS library.
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.Providing a database URL
All commands need to know which database to connect to. You can supply this in three ways:.env file
Create a
.env file in the directory where you run sqlx. The CLI loads it automatically:.env
Connection URL formats
| Database | Example URL |
|---|---|
| PostgreSQL | postgres://user:password@localhost/dbname |
| MySQL / MariaDB | mysql://user:password@localhost/dbname |
| SQLite | sqlite:./path/to/database.db |
Commands overview
The CLI is organized into three command groups:| Command | Description |
|---|---|
sqlx database | Create, drop, reset, or set up a database |
sqlx migrate | Add migration files, apply pending migrations, revert, and check status |
cargo sqlx prepare | Generate and validate offline query metadata for query! macros |
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.