Skip to main content

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.

SQLx uses Cargo feature flags to configure which async runtime, TLS backend, and database drivers are compiled in. You must choose at least one runtime feature. TLS and database features are optional but almost always needed in practice.

Adding SQLx to Cargo.toml

Pick the combination of runtime and TLS that matches your project. The examples below use PostgreSQL; substitute mysql or sqlite as needed.
[dependencies]
sqlx = { version = "0.8", features = ["runtime-tokio", "tls-native-tls", "postgres", "macros"] }
tokio = { version = "1", features = ["full"] }
You must select exactly one runtime feature. Combining multiple runtime features in the same dependency is not supported and will produce a compile error.

Runtime features

SQLx is runtime agnostic. Select the runtime that your application uses.
FeatureRuntimeNotes
runtime-tokioTokioMost widely used async runtime in the Rust ecosystem. Actix-web is compatible with Tokio and does not require a separate feature.
runtime-async-stdasync-stdAlternative runtime with a standard-library-like API surface.
runtime-async-global-executorasync-global-executorExecutor based on async-executor and blocking.
runtime-smolsmolLightweight async runtime.

TLS features

TLS is supported for PostgreSQL, MySQL, and MariaDB. SQLite does not use network connections and does not require a TLS feature.
FeatureBackendNotes
tls-native-tlsnative-tlsUses OpenSSL on Linux, SChannel on Windows, Secure Transport on macOS. Broadest compatibility with older TLS versions.
tls-rustls-ring-webpkirustls with ring + WebPKI CA rootsCross-platform pure Rust TLS. Uses WebPKI certificate roots bundled with the binary. Supports TLS 1.2 and 1.3 only.
tls-rustls-ring-native-rootsrustls with ring + system CA rootsSame as above but reads CA certificates from the operating system’s trust store.
tls-rustls-aws-lc-rsrustls with aws-lc-rsUses AWS’s fork of BoringSSL as the cryptographic backend instead of ring.
rustls only supports TLS 1.2 and 1.3. If your database server only supports older TLS versions, use tls-native-tls instead. Connections to such servers with a rustls backend will fail with a HandshakeFailure or CorruptMessage error.

Database features

FeatureDescription
postgresAdds support for the PostgreSQL database. Pure Rust driver.
mysqlAdds support for MySQL and MariaDB. Pure Rust driver.
mysql-rsaEnables RSA password encryption for caching_sha2_password / sha256_password auth when TLS is not used. Only enable this if you must connect without TLS to servers that require RSA auth. Prefer using TLS instead.
sqliteAdds support for SQLite using a bundled, statically linked libsqlite3. Enables sqlite-bundled, sqlite-deserialize, sqlite-load-extension, and sqlite-unlock-notify.
sqlite-unbundledSame as sqlite, but links SQLite from the system instead of the bundled version. Allows updating SQLite independently of SQLx or using a forked build. Requires SQLite 3.20.0 or newer on the system. May increase build time due to bindgen.
anyAdds the Any database driver, which selects a concrete driver at runtime based on the URL scheme. Useful for applications that need to support multiple databases from a single binary.

SQLite-specific features

These features apply only when sqlite or sqlite-unbundled is also enabled.
FeatureDescription
sqlite-bundledBundle and statically link SQLite. Included by the sqlite shorthand feature.
sqlite-deserializeEnables SqliteConnection::deserialize() and ::serialize(). Cannot be used with -DSQLITE_OMIT_DESERIALIZE. Requires -DSQLITE_ENABLE_DESERIALIZE on SQLite versions older than 3.36.0.
sqlite-load-extensionEnables SqliteConnectOptions::extension() and ::extension_with_entrypoint(). Required to use drivers.sqlite.unsafe-load-extensions from sqlx.toml. Cannot be used with -DSQLITE_OMIT_LOAD_EXTENSION.
sqlite-preupdate-hookEnables the sqlite3_preupdate_hook API. Requires -DSQLITE_ENABLE_PREUPDATE_HOOK, which is set automatically when using sqlite-bundled. Using this with sqlite-unbundled may cause linker failures if the system SQLite does not support it.
sqlite-unlock-notifyEnables internal handling of SQLITE_LOCKED_SHAREDCACHE. Requires -DSQLITE_ENABLE_UNLOCK_NOTIFY, which is set automatically when using sqlite-bundled.

Macro and derive features

FeatureDescription
macrosEnables the query!, query_as!, query_scalar!, and related compile-time verified query macros. Automatically enables derive.
deriveEnables the FromRow, Type, Encode, and Decode derive macros for mapping Rust structs to SQL types.
migrateEnables migration management and the migrate! macro for embedding migrations at compile time.
sqlx-tomlEnables parsing of sqlx.toml for configuring macros and migration behavior.

Type features

These features add support for additional Rust types when encoding and decoding SQL values.
FeatureCrateSQL types
jsonserde_jsonJSON, JSONB (PostgreSQL)
uuiduuidUUID (PostgreSQL), binary UUID (MySQL)
chronochronoDate and time types. Preferred by query! if both chrono and time are enabled.
timetimeAlternative date and time types.
bigdecimalbigdecimalNUMERIC / DECIMAL
rust_decimalrust_decimalNUMERIC / DECIMAL
ipnetipnetINET, CIDR (PostgreSQL)
ipnetworkipnetworkINET, CIDR (PostgreSQL)
mac_addressmac_addressMACADDR (PostgreSQL)
bit-vecbit-vecBIT, VARBIT (PostgreSQL)
bstrbstrBString for byte string handling
chrono and time cover overlapping SQL types. If both are enabled, query! will prefer chrono. Enabling both is generally not recommended unless you are migrating between them.

Optimizing compile times

The query! macro does significant work at compile time. You can reduce incremental build times by compiling sqlx-macros with optimizations in your development profile:
Cargo.toml
[profile.dev.package.sqlx-macros]
opt-level = 3
This setting applies only to the sqlx-macros crate and does not affect your own code’s debug information or compile-time error quality.

Offline mode

If you need to build without a live database (for example, in CI), you can use offline mode. Run cargo sqlx prepare to cache query metadata into a .sqlx directory, then set SQLX_OFFLINE=true at build time to use the cached data instead of connecting to the database. Offline mode is always available — it does not require a separate feature flag. See the sqlx-cli README for setup instructions.

Build docs developers (and LLMs) love