Safe · Extensible · Performant

Diesel is a Safe, Extensible
ORM and Query Builder

Diesel gets rid of the boilerplate for database interaction without sacrificing performance. It’s the most productive way to interact with databases in Rust.

security

Preventing Runtime Errors

Diesel eliminates the possibility of having a mismatch between your database schema and your Rust code. If it compiles, it’s valid. No more runtime surprises or production crashes due to typoed columns.

Compiler-Guaranteed Safety
speed

Built for Performance

Diesel is designed to be as fast as possible. It avoids heap allocations and runtime overhead where other ORMs might struggle. Zero-cost abstractions mean you pay for what you use.

extension

Extensible Querying

Need a custom SQL function? Diesel’s query builder is fully extensible. You can define your own operators or use complex SQL features while maintaining type safety.

bolt

Productive Workflow

With integrated migrations, CLI tools, and automatic schema generation, Diesel streamlines your entire development lifecycle from the first table to production.

PostgreSQL MySQL SQLite

See it in action

Compare how Diesel transforms complex SQL into readable, safe Rust code.

Simple Queries

Simple queries are a complete breeze. Loading all users from a database:

Rust
				
					
						users::table.load(&mut connection)
					
				
		
Executed SQL
				
					
						SELECT * FROM users;
					
				
		

Loading all the posts for a user:

Rust
				
					
						Post::belonging_to(user).load(&mut connection)
					
				
		
Executed SQL
				
					
						SELECT * FROM posts WHERE user_id = 1;
					
				
		
Complex Queries

Diesel’s powerful query builder helps you construct queries as simple or complex as you need, at zero cost.

Rust
				
					
						let versions = Version::belonging_to(krate)
    .select(id)
    .order(num.desc())
    .limit(5);

let downloads = version_downloads
    .filter(date.gt(now - 90.days()))
    .filter(version_id.eq(any(versions)))
    .order(date)
    .load::<Download>(&mut conn)?;
					
				
		
Executed SQL
				
					
						SELECT version_downloads.*
  WHERE date > (NOW() - '90 days')
    AND version_id = ANY(
      SELECT id FROM versions
        WHERE crate_id = 1
        ORDER BY num DESC
        LIMIT 5
    )
  ORDER BY date
					
				
		
Less Boilerplate

Diesel codegen generates boilerplate for you. It lets you focus on your business logic, not mapping to and from SQL rows.

With Diesel:

Rust
				
					
						#[derive(Queryable)]
pub struct Download {
    id: i32,
    version_id: i32,
    downloads: i32,
    counted: i32,
    date: SystemTime,
}
					
				
		

Without Diesel:

Rust
				
					
						pub struct Download {
    id: i32,
    version_id: i32,
    downloads: i32,
    counted: i32,
    date: SystemTime,
}

impl Download {
    fn from_row(row: &Row) -> Download {
        Download {
            id: row.get("id"),
            version_id: row.get("version_id"),
            downloads: row.get("downloads"),
            counted: row.get("counted"),
            date: row.get("date"),
        }
    }
}
					
				
		
Inserting Data

It’s not just about reading data. Diesel makes it easy to use structs for new records.

Rust
				
					
						#[derive(Insertable)]
#[diesel(table_name = users)]
struct NewUser<'a> {
    name: &'a str,
    hair_color: Option<&'a str>,
}

let new_users = vec![
    NewUser { name: "Sean", hair_color: Some("Black") },
    NewUser { name: "Gordon", hair_color: None },
];

insert_into(users)
    .values(&new_users)
    .execute(&mut connection);
					
				
		
Executed SQL
				
					
						INSERT INTO users (name, hair_color) VALUES
  ('Sean', 'Black'),
  ('Gordon', DEFAULT)
					
				
		
Updating Data

Diesel’s codegen can generate several ways to update a row, letting you encapsulate your logic in the way that makes sense for your app.

Modifying a struct:

Rust
				
					
						post.published = true;
post.save_changes(&mut connection);
					
				
		

One-off batch changes:

Rust
				
					
						update(users.filter(email.like("%@spammer.com")))
    .set(banned.eq(true))
    .execute(&mut connection)
					
				
		

Using a struct for encapsulation:

Rust
				
					
						update(Settings::belonging_to(current_user))
    .set(&settings_form)
    .execute(&mut connection)
					
				
		
Ergonomic Multidatabase support

Diesel allows to ergonomically abstract over different database backends while keeping all of it’s compile time guarantees.

Rust
				
					
						#[derive(diesel::MultiConnection)]
enum DatabaseConnection {
    Sqlite(diesel::SqliteConnection),
    Postgres(diesel::PgConnection),
}

let mut connection = DatabaseConnection::establish("postgres://localhost/diesel")?;

let all_users = users::table.load::<User>(connection)?;

match connection {
    DatabaseConnection::Sqlite(connection) => {
        perform_sqlite_specific_query(connection)?;
    }
    DatabaseConnection::Postgres(connection) => {
        perform_postgres_specific_query(connection)?;
    }
}
					
				
		
Ergonomic Raw SQL

There will always be certain queries that are just easier to write as raw SQL, or can’t be expressed with the query builder. Even in these cases, Diesel provides an easy to use API for writing raw SQL.

Rust
				
					
						#[derive(QueryableByName)]
#[diesel(table_name = users)]
struct User {
    id: i32,
    name: String,
    organization_id: i32,
}

sql_query(include_str!("complex_users_by_organization.sql"))
    .bind::<Integer, _>(organization_id)
    .bind::<BigInt, _>(offset)
    .bind::<BigInt, _>(limit)
    .load::<User>(&mut conn)?;
					
				
		

Trusted in Production

Diesel powers the most critical parts of the Rust ecosystem.

View all case studies
crates.io

crates.io

crates.io serves as a central registry for sharing “crates”, which are packages or libraries written in Rust that you can use to enhance your projects. This repository contains the source code and infrastructure for the crates.io website, including both frontend and backend components. It uses Diesel as central component to store crate metadata in a database.

PostgreSQL
Vaultwarden

Vaultwarden

Vaultwarden is a alternative server implementation of the Bitwarden Client API, written in Rust and compatible with official Bitwarden clients [disclaimer], perfect for self-hosted deployment where running the official resource-heavy service might not be ideal. Diesel is used to store sensitive data in their backend application.

PostgreSQL SQLite MySQL
Lemmy

Lemmy

Lemmy is a link aggregator and forum for the fediverse. It’s similar to sites like Reddit, Lobste.rs, or Hacker News: you subscribe to forums you’re interested in, post links and discussions, then vote, and comment on them. It’s a well established software for the fediverse using Diesel in the backend

PostgreSQL

You wanna add a notable project using Diesel?

Do you have found some cool project that should be linked here? Submit an issue here.

Contribute to Diesel

Help us build the most productive way to interact with databases in Rust.

menu_book

Documentation

Improve our guides, tutorials, and API docs to make Diesel accessible.

terminal

Core Engine

Add new backends or optimize query builders for peak performance.

web

Website

Improve the diesel.rs website design, accessibility, and UX.

groups

Community

Help with knowledge sharing and welcoming new users on GitHub.

Sponsors

Your support helps maintain Diesel and ensures it remains the best ORM for the Rust ecosystem.