13 releases

0.5.13 Apr 15, 2022
0.5.11 Feb 18, 2022
0.5.10 Dec 30, 2021
0.5.9 Oct 1, 2021
0.1.0-beta.1 Jul 27, 2020

#77 in Database interfaces

Download history 4746/week @ 2022-01-30 4185/week @ 2022-02-06 4479/week @ 2022-02-13 4429/week @ 2022-02-20 4581/week @ 2022-02-27 5317/week @ 2022-03-06 5354/week @ 2022-03-13 4059/week @ 2022-03-20 4608/week @ 2022-03-27 3929/week @ 2022-04-03 3976/week @ 2022-04-10 3844/week @ 2022-04-17 3635/week @ 2022-04-24 3615/week @ 2022-05-01 3436/week @ 2022-05-08 3968/week @ 2022-05-15

14,917 downloads per month


1.5K SLoC


SQLx's associated command-line utility for managing databases, migrations, and enabling "offline" mode with sqlx::query!() and friends.


With Rust toolchain

# supports all databases supported by SQLx
$ cargo install sqlx-cli

# only for postgres
$ cargo install sqlx-cli --no-default-features --features native-tls,postgres

# use vendored OpenSSL (build from source)
$ cargo install sqlx-cli --features openssl-vendored

# use Rustls rather than OpenSSL (be sure to add the features for the databases you intend to use!)
$ cargo install sqlx-cli --no-default-features --features rustls


All commands require that a database url is provided. This can be done either with the --database-url command line option or by setting DATABASE_URL, either in the environment or in a .env file in the current working directory.

For more details, run sqlx <command> --help.

# Postgres

Create/drop the database at DATABASE_URL

sqlx database create
sqlx database drop

Create and run migrations

sqlx migrate add <name>

Creates a new file in migrations/<timestamp>-<name>.sql. Add your database schema changes to this new file.

sqlx migrate run

Compares the migration history of the running database against the migrations/ folder and runs any scripts that are still pending.

Users can provide the directory for the migration scripts to sqlx migrate subcommands with the --source flag.

sqlx migrate info --source ../relative/migrations

Reverting Migrations

If you would like to create reversible migrations with corresponding "up" and "down" scripts, you use the -r flag when creating new migrations:

$ sqlx migrate add -r <name>
Creating migrations/20211001154420_<name>.up.sql
Creating migrations/20211001154420_<name>.down.sql

After that, you can run these as above:

$ sqlx migrate run
Applied migrations/20211001154420 <name> (32.517835ms)

And reverts work as well:

$ sqlx migrate revert
Applied 20211001154420/revert <name>

Note: attempting to mix "simple" migrations with reversible migrations with result in an error.

$ sqlx migrate add <name1>
Creating migrations/20211001154420_<name>.sql

$ sqlx migrate add -r <name2>
error: cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations

Enable building in "offline mode" with query!()

Note: must be run as cargo sqlx.

cargo sqlx prepare

Saves query metadata to sqlx-data.json in the current directory; check this file into version control and an active database connection will no longer be needed to build your project.

Has no effect unless the offline feature of sqlx is enabled in your project. Omitting that feature is the most likely cause if you get a sqlx-data.json file that looks like this:

    "database": "PostgreSQL"

cargo sqlx prepare --check

Exits with a nonzero exit status if the data in sqlx-data.json is out of date with the current database schema and queries in the project. Intended for use in Continuous Integration.

Force building in offline mode

To make sure an accidentally-present DATABASE_URL environment variable or .env file does not result in cargo build (trying to) access the database, you can set the SQLX_OFFLINE environment variable to true.

If you want to make this the default, just add it to your .env file. cargo sqlx prepare will still do the right thing and connect to the database.

Include queries behind feature flags (such as queryies inside of tests)

In order for sqlx to be able to find queries behind certain feature flags you need to turn them on by passing arguments to rustc.

This is how you would turn all targets and features on.

cargo sqlx prepare -- --all-targets --all-features


~498K SLoC