Backend development

Stage 2 · Lesson 9 of 11

SQLx: pools and checked queries

Relational databases and SQL

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

Practise

Keep handy

  • Query macros, pools, transactions, migrations and offline mode, with PostgreSQL examples

Also worth your time

Mark this stage as not done?