This guide walks you through adding SQLx to an existing Rust project, creating a connection pool, and running both runtime and compile-time verified queries against a PostgreSQL database. The same patterns apply to MySQL, MariaDB, and SQLite — differences are called out inline.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
Add SQLx to Cargo.toml
Add SQLx as a dependency with your chosen runtime, TLS backend, and database driver. The following example uses Tokio and PostgreSQL:Adjust the features to match your stack:
Cargo.toml
You must choose exactly one runtime feature (
runtime-tokio, runtime-async-std, etc.) and, if connecting over TLS, one TLS feature (tls-native-tls or one of the tls-rustls-* variants). See Installation and feature flags for the full reference.Set the DATABASE_URL environment variable
SQLx reads For convenience, you can store this in a Connection string formats by database:
DATABASE_URL at both compile time (for the query! macro) and runtime (for your application code). Set it to a connection string for your database:.env file at the root of your project. SQLx picks it up automatically via the dotenvy crate:.env
| Database | Format |
|---|---|
| PostgreSQL | postgres://user:password@host/database |
| MySQL / MariaDB | mysql://user:password@host/database |
| SQLite (file) | sqlite:./path/to/database.db |
| SQLite (in-memory) | sqlite::memory: |
Create a connection pool
Most applications should use a connection pool rather than individual connections. Create one using In practice, read the connection URL from an environment variable:For other databases, use the corresponding pool type:
PgPoolOptions and share it across your application:src/main.rs
src/main.rs
Run a basic query
Use To fetch multiple rows and access columns by name, use
sqlx::query() to run a parameterized query. Parameters use $1, $2, etc. for PostgreSQL, and ? for MySQL/MariaDB and SQLite:fetch_all and row.get():SQLx always uses prepared statements for
sqlx::query() and its variants. Parameters are never interpolated on the client side, which eliminates SQL injection by design.Use the query! macro for compile-time verification
The Bind parameters are compile-time checked for count and type:
query! macro verifies your SQL against a live database at compile time. It checks that the SQL is valid for your database and that you are binding the right number and types of parameters. The result columns become fields on an anonymous struct:Use FromRow to map results to a struct
When you need a named output type, derive You can also use
sqlx::FromRow on a struct and use query_as! or sqlx::query_as():sqlx::query_as() (without the macro) when you do not need compile-time SQL verification:FromRow is available when the derive feature is enabled. The derive feature is automatically enabled by the macros feature.Next steps
Installation and feature flags
Full reference for every SQLx feature flag, including TLS backends and type integrations.
Introduction
Learn what makes SQLx unique and how compile-time query verification works under the hood.