SQLx Documentation
The docs for SQLx, the async SQL toolkit most Axum projects use with PostgreSQL. Plain SQL with query macros checked against your real schema at compile time, connection pools, transactions, migrations and an offline mode for CI.
17.5k (transact-rs/sqlx)
GitHub · Sept 2026
0.9.0
crates.io · Sept 2026
153 million
crates.io · Sept 2026
Guide
Web Development
SQLx maintainers
A week for pools, macros and migrations
Intermediate
Free
What SQLx is
SQLx is an async SQL crate for Rust that supports PostgreSQL, MySQL and SQLite. It is not an ORM: you write plain SQL. Its best-known feature is the query! and query_as! macros, which check each query against a real database while your code compiles, so a misspelt column or a wrong Rust type fails the build instead of a request. It also provides connection pools such as PgPool, transactions, and migrations through the sqlx-cli tool and the migrate! macro.
Where to read
The API documentation on docs.rs is the main reference, with the README and the examples folder in the GitHub repository filling in the details. The sqlx-cli README explains migrations and offline mode. The project moved to the transact-rs GitHub organisation with the 0.9 release, and the 0.9 changelog is worth reading if you follow older tutorials, since query() now only accepts static strings unless you wrap a built string in AssertSqlSafe.
Is it worth learning?
For a Rust backend on PostgreSQL, it is the most common choice, and it keeps your SQL visible, which suits anyone who learnt SQL properly first. If you would rather build queries in Rust code, Diesel is the main alternative.
The parts that matter most
- Pools: create one
PgPoolwithPgPoolOptionsat startup, setmax_connections, and share it through your Axum state. - Query macros:
query_as!maps rows onto your structs and needsDATABASE_URLset when you build. - Offline mode:
cargo sqlx preparesaves query metadata to a.sqlxfolder; commit it so CI can compile without a database. - Migrations:
sqlx migrate add,sqlx migrate run, and-rfor reversible up and down files. - Testing:
#[sqlx::test]creates a fresh database for each test and applies your migrations automatically.
Prerequisites
SQL and PostgreSQL basics, Rust with Result and ?, and async/await on Tokio.
Free or paid?
Free and open source.
What you can do after it
Run type-checked SQL from an Axum API, manage schema changes with migrations, use transactions correctly and test against a real database in CI.
Common questions
Why does my project not compile without a database? The macros need either DATABASE_URL or the prepared .sqlx folder.
Can I build dynamic queries? Yes, with QueryBuilder, which keeps values as bound parameters.
Ready to start?
Opens on SQLx maintainers in a new tab.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
Follow along
New writing, resources and project ideas land here first.