Stage 2 · Lesson 9 of 11
SQLx: pools and checked queries
Most Rust backends talk to through , which runs plain SQL asynchronously on . You create one PgPool, a pool of open connections, at startup and share it through your app state. Placeholders like $1 values separately from the SQL text, which protects you from .
The query! and query_as! macros check every query against a real database while the code compiles, so a misspelt column or a wrong type fails the build. cargo sqlx prepare saves that information to a .sqlx folder, so CI can build without a database. Diesel is the main alternative, a query builder and ORM with its own style.
Common trapbuilding SQL with format! from user input: it invites injection, and recent SQLx versions make you wrap such strings in AssertSqlSafe so reviewers notice.
A question to check yourself, an AI practice session and 3 cheat sheet entries come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Relational Database Design and Modeling
Design Gurus · Intermediate · $98 one-time
The modelling and normalisation decisions that stay with a codebase for years
- CourseFreeCS50's Introduction to Databases with SQL
Harvard University · Beginner
Free course from querying through schema design, indexes, transactions and scaling
Practise
Keep handy
Also worth your time
- Database Indexing Explained: B-Tree, Hash, Bitmap, R-Tree, and When to Use Each (opens in a new tab)Read
B-tree and composite indexes, plus the leftmost-prefix rule that trips people up
What sits under your queries: storage engines, transactions and isolation levels