0PricingLogin
Learn Rust Coding · Lesson

Database Integration

Persist data.

Persisting Data

So far our API stored data in memory, which vanishes on restart. Real services need a database. In this lesson we connect an Axum API to PostgreSQL using sqlx, an async, compile-time-checked SQL toolkit for Rust.

You will learn connection pools, queries, mapping rows to structs, and using the pool as shared state.

Adding sqlx

Add sqlx with the features you need: a runtime, TLS, and a database driver. Below is a Postgres setup using Tokio.

// Cargo.toml
// [dependencies]
// sqlx = { version = "0.7", features = [
//   "runtime-tokio", "tls-rustls", "postgres", "macros"
// ] }

All lessons in this course

  1. Project Setup
  2. Endpoints and Models
  3. Database Integration
  4. Testing the API
← Back to Learn Rust Coding