SkillAgentSearch skills...

Sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.

Install / Use

/learn @launchbadge/Sqlx
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<h1 align="center">SQLx</h1> <div align="center"> <strong> 🧰 The Rust SQL Toolkit </strong> </div> <br /> <div align="center"> <!-- Github Actions --> <a href="https://github.com/launchbadge/sqlx/actions/workflows/sqlx.yml?query=branch%3Amain"> <img src="https://img.shields.io/github/actions/workflow/status/launchbadge/sqlx/sqlx.yml?branch=main&style=flat-square" alt="actions status" /></a> <!-- Version --> <a href="https://crates.io/crates/sqlx"> <img src="https://img.shields.io/crates/v/sqlx.svg?style=flat-square" alt="Crates.io version" /></a> <!-- Discord --> <a href="https://discord.gg/uuruzJ7"> <img src="https://img.shields.io/discord/665528275556106240?style=flat-square" alt="chat" /></a> <!-- Docs --> <a href="https://docs.rs/sqlx"> <img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs docs" /></a> <!-- Downloads --> <a href="https://crates.io/crates/sqlx"> <img src="https://img.shields.io/crates/d/sqlx.svg?style=flat-square" alt="Download" /> </a> </div> <div align="center"> <h4> <a href="#install"> Install </a> <span> | </span> <a href="#usage"> Usage </a> <span> | </span> <a href="https://docs.rs/sqlx"> Docs </a> <span> | </span> <a href="https://github.com/launchbadge/sqlx/wiki/Ecosystem"> Ecosystem </a> <span> | </span> <a href="https://discord.gg/uuruzJ7"> Discord </a> </h4> </div> <br /> <div align="center"> <small>Built with ❤️ by <a href="https://launchbadge.com">The LaunchBadge team</a></small> </div> <br /> <div align="center"> <h5>Have a question? Be sure to <a href="FAQ.md">check the FAQ first!</a></h5> </div> <br />

SQLx is an async, pure Rust<sub></sub> SQL crate featuring compile-time checked queries without a DSL.

  • Truly Asynchronous. Built from the ground-up using async/await for maximum concurrency.

  • Compile-time checked queries (if you want). See SQLx is not an ORM.

  • Database Agnostic. Support for PostgreSQL, MySQL, MariaDB, SQLite.

    • MSSQL was supported prior to version 0.7, but has been removed pending a full rewrite of the driver as part of our SQLx Pro initiative.
  • Pure Rust. The Postgres and MySQL/MariaDB drivers are written in pure Rust using zero unsafe<sub>††</sub> code.

  • Runtime Agnostic. Works on different runtimes (async-std / tokio / actix) and TLS backends (native-tls, rustls).

<small><small>

† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way we could be pure Rust for SQLite is by porting all of SQLite to Rust).

†† SQLx uses #![forbid(unsafe_code)] unless the sqlite feature is enabled. The SQLite driver directly invokes the SQLite3 API via libsqlite3-sys, which requires unsafe.

</small></small>


  • Cross-platform. Being native Rust, SQLx will compile anywhere Rust is supported.

  • Built-in connection pooling with sqlx::Pool.

  • Row streaming. Data is read asynchronously from the database and decoded on demand.

  • Automatic statement preparation and caching. When using the high-level query API (sqlx::query), statements are prepared and cached per connection.

  • Simple (unprepared) query execution including fetching results into the same Row types used by the high-level API. Supports batch execution and returns results from all statements.

  • Transport Layer Security (TLS) where supported (MySQL, MariaDB and PostgreSQL).

  • Asynchronous notifications using LISTEN and NOTIFY for PostgreSQL.

  • Nested transactions with support for save points.

  • Any database driver for changing the database driver at runtime. An AnyPool connects to the driver indicated by the URL scheme.

Install

SQLx is compatible with the async-std, tokio, and actix runtimes; and, the native-tls and rustls TLS backends. When adding the dependency, you must choose a runtime feature that is runtime + tls.

# Cargo.toml
[dependencies]
# PICK ONE OF THE FOLLOWING:

# tokio (no TLS)
sqlx = { version = "0.8", features = [ "runtime-tokio" ] }
# tokio + native-tls
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-native-tls" ] }
# tokio + rustls with ring and WebPKI CA certificates
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-rustls-ring-webpki" ] }
# tokio + rustls with ring and platform's native CA certificates
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-rustls-ring-native-roots" ] }
# tokio + rustls with aws-lc-rs
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-rustls-aws-lc-rs" ] }

# async-std (no TLS)
sqlx = { version = "0.8", features = [ "runtime-async-std" ] }
# async-std + native-tls
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-native-tls" ] }
# async-std + rustls with ring and WebPKI CA certificates
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-rustls-ring-webpki" ] }
# async-std + rustls with ring and platform's native CA certificates
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-rustls-ring-native-roots" ] }
# async-std + rustls with aws-lc-rs
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-rustls-aws-lc-rs" ] }

Cargo Feature Flags

For backward-compatibility reasons, the runtime and TLS features can either be chosen together as a single feature, or separately.

For forward compatibility, you should use the separate runtime and TLS features as the combination features may be removed in the future.

  • runtime-async-std: Use the async-std runtime without enabling a TLS backend.

  • runtime-tokio: Use the tokio runtime without enabling a TLS backend.

    • Actix-web is fully compatible with Tokio and so a separate runtime feature is no longer needed.
  • tls-native-tls: Use the native-tls TLS backend (OpenSSL on *nix, SChannel on Windows, Secure Transport on macOS).

  • tls-rustls: Use the rustls TLS backend (cross-platform backend, only supports TLS 1.2 and 1.3).

  • postgres: Add support for the Postgres database server.

  • mysql: Add support for the MySQL/MariaDB database server.

  • mssql: Add support for the MSSQL database server.

  • sqlite: Add support for the self-contained SQLite database engine with SQLite bundled and statically-linked.

  • sqlite-unbundled: The same as above (sqlite), but link SQLite from the system instead of the bundled version.

    • Allows updating SQLite independently of SQLx or using forked versions.
    • You must have SQLite installed on the system or provide a path to the library at build time. See the rusqlite README for details.
    • May result in link errors if the SQLite version is too old. Version 3.20.0 or newer is recommended.
    • Can increase build time due to the use of bindgen.
  • sqlite-preupdate-hook: enables SQLite's preupdate hook API.

    • Exposed as a separate feature because it's generally not enabled by default.
    • Using this feature with sqlite-unbundled may cause linker failures if the system SQLite version does not support it.
  • any: Add support for the Any database driver, which can proxy to a database driver at runtime.

  • derive: Add support for the derive family macros, those are FromRow, Type, Encode, Decode.

  • macros: Add support for the query*! macros, which allows compile-time checked queries.

  • migrate: Add support for the migration management and migrate! macro, which allow compile-time embedded migrations.

  • uuid: Add support for UUID.

  • chrono: Add support for date and time types from chrono.

  • time: Add support for date and time types from time crate (alternative to chrono, which is preferred by query! macro, if both enabled)

  • bstr: Add support for bstr::BString.

  • bigdecimal: Add support for NUMERIC using the bigdecimal crate.

  • rust_decimal: Add support for NUMERIC using the rust_decimal crate.

  • ipnet: Add support for INET and CIDR (in postgres) using the ipnet crate.

  • ipnetwork: Add support for INET and CIDR (in postgres) using the ipnetwork crate.

  • json: Add support for JSON and JSONB (in postgres) using the serde_json crate.

  • Offline mode is now always enabled. See sqlx-cli/README.md.

SQLx is not an ORM!

SQLx supports compile-time checked queries. It does not, however, do this by providing a Rust API or DSL (domain-specific language) for building queries. Instead, it provides macros that take regular SQL as input and ensure that it is valid for your database. The way this works is that SQLx connects to your development DB at compile time to have the database itself verify (and return some info on) your SQL queries. This has some potentially surprising implications:

  • Since SQLx never has to parse the SQL string itself, any syntax that the development DB accepts can be used (including things added by database extensions)
  • Due to the different amount of information databases let you retr

Related Skills

View on GitHub
GitHub Stars16.8k
CategoryData
Updated14m ago
Forks1.6k

Languages

Rust

Security Score

100/100

Audited on Mar 21, 2026

No findings