TheDocumentation 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.
query!, query_as!, and related macros verify your SQL at compile time by connecting to a real database. This works well on a developer machine, but CI servers and documentation build environments typically do not have a live database available. Offline mode solves this by caching the metadata that the macros need into a .sqlx directory that you check into version control.
How offline mode works
When you runcargo sqlx prepare, it compiles your project with a live database connection, captures the query metadata returned by the database for each query! invocation, and writes that metadata to .sqlx/. On subsequent builds where SQLX_OFFLINE=true is set (or where no DATABASE_URL is present), the macros read from .sqlx/ instead of talking to a database.
Generating the .sqlx cache
Run cargo sqlx prepare from your project root with DATABASE_URL set:
.sqlx/:
cargo sqlx prepare must be run as cargo sqlx, not as a standalone sqlx binary, because it needs access to Cargo’s build environment to find all query macro invocations.Workspace projects
For workspaces where multiple crates use query macros, pass--workspace to generate a single .sqlx directory at the workspace root rather than one per crate:
.sqlx directory into version control. All crates in the workspace will read from it during offline builds.
Checking .sqlx into version control
Add the .sqlx directory to your repository so that CI can build without a database:
.sqlx is committed, any machine that clones the repository can build the project with SQLX_OFFLINE=true.
Building in offline mode
SetSQLX_OFFLINE=true to tell SQLx to read from .sqlx instead of connecting to a database:
DATABASE_URL and .sqlx are present, DATABASE_URL takes precedence unless SQLX_OFFLINE=true is explicitly set. Add SQLX_OFFLINE=true to your .env file to make offline the default for local builds:
cargo sqlx prepare always connects to the database regardless of SQLX_OFFLINE, so this combination is safe.
Validating the cache in CI
Usecargo sqlx prepare --check to verify that .sqlx is up to date with the current database schema and queries. It exits with a non-zero status if anything is out of sync, making it suitable as a CI gate:
Git pre-commit hook
To keep.sqlx automatically up to date, install a pre-commit hook that runs cargo sqlx prepare before each commit:
This hook does not block the commit if
cargo sqlx prepare fails — it is an ergonomic choice to avoid blocking work in progress. Use cargo sqlx prepare --check in CI as the authoritative gate.Building on docs.rs
docs.rs does not have access to your database and does not setSQLX_OFFLINE. You can detect the docs.rs environment via the DOCS_RS environment variable and force offline mode from a build.rs script.
Generate the .sqlx cache
Run
cargo sqlx prepare locally and commit the .sqlx directory to your repository.Including feature-flagged queries
By default,cargo sqlx prepare only processes queries reachable under the default feature set and the default compilation targets. To include queries that are behind feature flags or inside #[cfg(test)] blocks, pass additional arguments to Cargo after --:
.sqlx without this flag.