Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt

Use this file to discover all available pages before exploring further.

SQLx is the Rust SQL toolkit that checks your queries at compile time — without forcing you to use a domain-specific language. Write plain SQL for PostgreSQL, MySQL, MariaDB, or SQLite, and SQLx will verify your queries against a real database before your code ever ships.

Quickstart

Connect to a database and run your first query in minutes

Installation

Add SQLx to your Cargo.toml with the right features

Compile-time Queries

Learn how the query! macro checks SQL at compile time

API Reference

Explore the full SQLx public API surface

Why SQLx?

SQLx is not an ORM. It gives you full control of your SQL while eliminating an entire class of runtime errors through compile-time verification.

Truly Async

Built from the ground up with async/await. Works with Tokio, async-std, and actix runtimes.

Compile-time Safety

The query! macro connects to your dev database at build time to verify SQL correctness and types.

Pure Rust Drivers

PostgreSQL and MySQL drivers are written in pure Rust with zero unsafe code.

Multi-database

First-class support for PostgreSQL, MySQL, MariaDB, and SQLite with a shared API.

Get started

1

Add SQLx to your project

Add SQLx to your Cargo.toml with your chosen runtime and database features:
Cargo.toml
[dependencies]
sqlx = { version = "0.8", features = ["runtime-tokio", "postgres"] }
2

Connect to your database

Create a connection pool using your database URL:
use sqlx::postgres::PgPoolOptions;

let pool = PgPoolOptions::new()
    .max_connections(5)
    .connect("postgres://postgres:password@localhost/mydb")
    .await?;
3

Run your first query

Execute a compile-time verified query with the query! macro:
let row = sqlx::query!("SELECT id, name FROM users WHERE id = $1", user_id)
    .fetch_one(&pool)
    .await?;

println!("User: {}", row.name);

Explore by topic

Connection Pool

Manage database connections efficiently with built-in pooling

Migrations

Create and manage database schema migrations

Database Drivers

Database-specific features for PostgreSQL, MySQL, and SQLite

Type System

Map SQL types to Rust types, including custom types

SQLx CLI

Command-line tool for database management and offline mode

Offline Mode

Build without a database connection using cached query metadata

Build docs developers (and LLMs) love