SQLx is a Rust crate for async database access with optional compile-time SQL validation. It supports PostgreSQL, MySQL, MariaDB, and SQLite without requiring a query DSL.
Overview
SQLx is an async, pure Rust SQL library aimed at applications that want direct SQL strings rather than an ORM-style API. It can verify queries at compile time through macros that check SQL against a development database, while still accepting any syntax the database understands.
The crate is database-agnostic across PostgreSQL, MySQL, MariaDB, and SQLite, with drivers largely written in Rust. It works with common async runtimes and TLS backends, includes connection pooling, streams rows asynchronously, and prepares and caches statements when using the high-level query API.
Applications can run prepared or simple queries, map rows into domain types, and use macros such as query! for typed anonymous records or query_as! for named output types. Migrations can be embedded at compile time, and offline mode caches analysis so builds do not always need a live database.
SQLx explicitly states it is not an ORM: it does not offer a Rust query builder DSL. For teams comparing approaches, the project points readers to separate ecosystem resources for asynchronous ORMs.
Key features
- Async await database access
- Compile-time checked query macros
- PostgreSQL, MySQL, MariaDB, SQLite
- Built-in connection pooling
- Runtime choice for tokio or async-std
- TLS support for networked databases
- Embedded migrations and offline compile mode
- PostgreSQL LISTEN and NOTIFY support
Use cases
- Rust services needing async SQL access
- Compile-time SQL validation in CI
- Multi-database Rust codebases
- Streaming large query result sets