Database Integration - Axum - Complete Documentation by ntxm.org

sqlx overview

Learn what sqlx is, how it talks to Postgres from an Axum app, and how its queries, pools, and compile-time checks fit together

SeaORM overview

Learn what SeaORM is, how its entities map tables to Rust structs, and how it builds on sqlx and SeaQuery inside an Axum app

diesel overview

Learn what diesel is, how its table macro checks queries at compile time, and how its synchronous design fits an async Axum app

DB crate async support

Learn which Rust database crates are async and which are sync, what blocking means inside an Axum handler, and how to use each one correctly

Compile-time checked queries

Learn how the sqlx query macro checks SQL against a live database while your program compiles, and how offline mode keeps that check without a server

ORM mapping comparison

Learn how sqlx, SeaORM, and diesel each turn a database row into a Rust value, how they map writes back, and how to pick between them

The sqlx crate

Learn what the sqlx crate is, how to add it to an Axum project, and what it gives you for talking to PostgreSQL from Rust

The postgres feature

Learn what the postgres feature switches on in sqlx, which types it unlocks, and how to tell when it is missing

The runtime-tokio feature

Learn what the runtime-tokio feature does in sqlx, why an async database crate needs a runtime, and how it connects to the Tokio runtime Axum already uses

PgPool

Learn what a PgPool connection pool is, how to create one with PgPool connect, share it through Axum state, and close it cleanly

PgPoolOptions

Learn how PgPoolOptions configures a sqlx connection pool, what max_connections, min_connections, acquire_timeout, idle_timeout, and max_lifetime do, and what each one defaults to

DATABASE_URL

Learn what the DATABASE_URL connection string is, how to read each part of a postgres URL, where to keep it, and what happens when it is missing or wrong

Connection pool size

Learn how to choose max_connections and min_connections for a sqlx pool, what happens when the pool runs out, and how to read pool size under load

The query macro

Learn how the sqlx query macro checks SQL at compile time, what anonymous record it returns, and how fetch_all, fetch_one, fetch_optional, and execute work

The query_as macro

Learn how the sqlx query_as macro maps rows into a named Rust struct, how its fields must match the query columns, and when to use it instead of query

sqlx migrations

Learn what a sqlx migration is, how versioned SQL files build a database schema step by step, and how the _sqlx_migrations table tracks what has run

sqlx-cli

Learn what the sqlx-cli command line tool is, how to install it with cargo install sqlx-cli, and what its database, prepare, and migrate commands do

sqlx migrate

Learn how to use sqlx migrate add, run, info, and revert to create versioned migration files, apply them in order, inspect their state, and roll one back

SeaORM crate

Learn what the SeaORM crate is, how to add it to an Axum project with the right features, and how to open your first database connection.

SeaORM entities

Learn what a SeaORM entity is, how the dense entity format maps a table to a Rust struct, and how to write and generate entity files.

SeaORM migrations

Learn how SeaORM migrations version database schema changes, how to write a migration that creates the users table, and how to run it.

SeaQuery

Learn what SeaQuery is, how its builders construct SQL from Rust method calls, and how to render and run a built statement against Postgres.

SELECT builder

Read rows from the users table with Entity find and its filter order limit and offset chain

INSERT builder

Create rows in the users table with ActiveModel and the insert methods

UPDATE builder

Change existing rows with ActiveModel update and update many

DELETE builder

Remove rows from the users table with model delete and delete many

Connection options

Tune the SeaORM connection pool with ConnectOptions for timeouts size and logging

DB module separation

Keep database code in its own module with one way dependencies and shared state

CRUD CREATE POST

Learn how a POST handler inserts a new user row into Postgres with sqlx and returns the created user with status 201

CRUD READ GET one

Learn how a GET handler reads one user row from Postgres by id with sqlx and returns 404 when the row does not exist

CRUD READ GET list

Learn how a GET handler lists every user row from Postgres with sqlx fetch_all and returns an empty JSON array when the table is empty

CRUD UPDATE PUT

Learn how a PUT handler replaces a whole user row in Postgres with sqlx and returns 404 when the id does not exist

CRUD UPDATE PATCH

Learn how a PATCH handler changes only the fields a client sends with sqlx COALESCE and leaves the rest of the user row untouched

CRUD DELETE

Learn how a DELETE handler removes one user row from Postgres with sqlx and returns 204 when the row is gone and 404 when it never existed

users table

Learn how the users table is defined in Postgres, what each column means, and how the table matches the Rust User struct the handlers use

Database error variants

Learn how sqlx database errors map onto the shared AppError type, which failures become 404 or 500, and what the server log keeps

sqlx query macro in handlers

Learn how the sqlx query macro checks SQL at compile time inside Axum handlers and returns rows as anonymous records

INSERT with RETURNING

Learn how the Postgres RETURNING clause gives an insert handler the new row back in the same statement, including its database generated id

Embedding migrations with sqlx migrate macro

Learn how the sqlx migrate macro embeds SQL migration files into the binary and runs them at startup so the database schema is created by the app itself

Pagination with limit and offset

Learn how LIMIT and OFFSET page through a user list in Axum handlers, with page numbers, a page size cap, and a total count

Unique constraint to 409 mapping

Learn how a Postgres unique violation with error code 23505 becomes a 409 Conflict response through a new AppError variant, in the same single shape JSON body

Missing row to 404 mapping

Learn how a query that finds no row becomes a 404 response through the RowNotFound error, the shared AppError type, and the single shape JSON body