sqlx
23 Feb 2026Database drivers often trade safety for convenience.
sqlx does something unusual:
It validates SQL queries at compile time.
What Problem Does sqlx Solve?
- Async database access
- Strong typing
- Compile-time query validation
Without an ORM.
Minimal Example
Cargo.toml
[dependencies]
sqlx = { version = "0.7", features = ["postgres", "runtime-tokio"] }
tokio = { version = "1", features = ["full"] }main.rs
use sqlx::postgres::PgPoolOptions;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let pool = PgPoolOptions::new()
.connect("postgres://postgres:password@localhost/db")
.await?;
let row: (i64,) = sqlx::query_as("SELECT 1")
.fetch_one(&pool)
.await?;
println!("result = {:?}", row);
Ok(())
}What’s Actually Happening?
With the query! macro, sqlx can:
- Connect to your DB at build time
- Validate SQL
- Infer result types
That’s rare in systems languages.
Should You Use It?
If you want SQL without an ORM:
Yes.
It’s disciplined and powerful.