The postgres feature
Learn what the postgres feature switches on in sqlx, which types it unlocks, and how to tell when it is missing
The postgres feature is the switch that gives SQLx its PostgreSQL driver. A driver is the code that speaks a particular database's network language, and SQLx ships one driver per database it supports. Without this feature, the crate compiles but knows nothing about PostgreSQL: no PostgreSQL pool, no PostgreSQL row type, and no way to open a PostgreSQL connection. With it, the whole sqlx::postgres module appears, and the rest of this cluster can use it.
Where This Fits
The crate page added SQLx with two features, postgres and runtime-tokio. This page takes the first of those two and opens it up. The feature is one word in Cargo.toml, but it decides which types exist, which connection strings the crate understands, and which error messages you get when something is wrong. Understanding it now saves a confusing half hour later, when a missing feature shows up disguised as a missing type.
Think of the feature as a language pack. SQLx on its own is a translator with no languages installed: it knows how to translate, but it has no PostgreSQL vocabulary. Turning on postgres installs the PostgreSQL vocabulary, the grammar of the wire protocol, the type mappings, and the error messages. The translator itself does not change, and neither does the way you write queries. Only the language it can speak changes.
The Smallest Working Version
The feature is already on in the project from the crate page. Here it is on its own line so the effect is visible:
[dependencies]
sqlx = { version = "0.9", features = ["postgres", "runtime-tokio", "macros", "derive"] }
The proof that the feature is on is that this code compiles:
use sqlx::postgres::{PgPool, PgPoolOptions};
pub async fn create_pool(database_url: &str) -> Result<PgPool, sqlx::Error> {
PgPoolOptions::new()
.max_connections(5)
.connect(database_url)
.await
}
PgPool and PgPoolOptions both live under sqlx::postgres, and that module only exists when the feature is on. Run cargo check and a clean finish means the feature is active. Nothing here touches a database yet: compiling is the check, and the database comes later.
What the Feature Unlocks
The feature switches on four groups of things. A beginner does not need all four at once, but knowing they exist makes the later pages land on familiar ground.
The pool and connection types come first. PgPool is the connection pool for PostgreSQL, PgPoolOptions is its builder, and PgConnection is a single connection. These are the types the app holds, configures, and hands to handlers. They are aliases for the generic Pool<Postgres> and PoolOptions<Postgres> types, specialised for PostgreSQL, which is why they read as plain names instead of angle-bracket generics.
The row and value types come second. PgRow is one row returned by a query, and the PostgreSQL type mappings say how each database type becomes a Rust type: SERIAL becomes i32, TEXT becomes String, TIMESTAMPTZ becomes a time type from the chrono or time crates when those features are on. The query macros use these mappings to check that the Rust struct on the receiving end matches the columns the query returns.
The connection options come third. PgConnectOptions holds the parsed form of a connection string: host, port, username, password, database name, and extras such as the SSL mode and the statement cache size. Most programs never build one by hand, because the pool parses the URL string for them, but the type is there when a program needs to set an option the URL cannot express.
The error type comes fourth. sqlx::Error is shared across all databases, but the database errors it wraps carry PostgreSQL's own messages: password authentication failed, database "no_such_db" does not exist, relation "users" does not exist. Those messages come from the server, through the driver, unchanged. When a program prints a SQLx error, the words after the colon are PostgreSQL speaking.
One Crate, Four Databases
SQLx supports PostgreSQL, MySQL, MariaDB, and SQLite from the same crate, and each one is a feature: postgres, mysql, sqlite. The project turns on only the one it uses. That is why the dependency line is explicit instead of turning everything on: each driver adds compile time and binary size, and a project that only talks to PostgreSQL has no use for the MySQL driver.
The feature list is additive, which means features only add code and never remove it. Turning on postgres and mysql together is legal, and the crate then offers both sqlx::postgres and sqlx::mysql. Nothing breaks, nothing conflicts, and the program picks a driver per pool by choosing which pool type to build. This project keeps one driver because it talks to one database, and the single-driver setup is the one these pages check.
A related point: the any feature, which is on by default, adds a driver that picks the database from the URL scheme at runtime. It is how a program can accept postgres:// or sqlite:// URLs without recompiling. This project does not use it, because the database is fixed, and the pool type says so in its name.
The Driver Is Pure Rust
The PostgreSQL driver is written entirely in Rust, with no C code and no system library. That matters for a beginner in one concrete way: there is nothing to install besides the crate. Other database libraries link against libpq, the C client library for PostgreSQL, which means installing a system package and pointing the build at it. SQLx implements the PostgreSQL wire protocol itself, in Rust, so cargo check is the whole installation.
The wire protocol is the sequence of bytes the client and the server exchange over the network: a startup message with the username and database, an authentication exchange, then query messages and row replies. The driver builds those bytes, reads the replies, and turns them into Rust values. A beginner never sees the bytes, but knowing they exist explains two behaviours: the driver can connect to any PostgreSQL server that speaks the protocol, whatever operating system it runs on, and a connection failure is a network failure first and a SQL failure second.
No TLS Feature Needed for a Local Database
A local PostgreSQL connection needs no TLS feature. TLS, short for Transport Layer Security, is the encryption layer that protects traffic between two machines. On one machine, between the app and a local server, there is nothing to protect from the network, and PostgreSQL accepts plain connections.
SQLx still handles the TLS question gracefully. The default SSL mode is Prefer, which means the driver first offers an encrypted connection and, if the server declines, falls back to a plain one. A local server declines, the driver falls back, and the connection works with no extra features. The tls-rustls and tls-native-tls features only become necessary when the server requires encryption, which is a hosted database, not a local one.
Prefer Means Fall Back, Not Fail:
The default SSL mode is Prefer, so a plain local connection works with no TLS feature. If a hosted database ever requires encryption, the fix is one feature, tls-rustls, added to the list. Until then, the absence of a TLS feature is correct, not an oversight.
What Happens Without the Feature
Remove postgres from the feature list and the project still compiles, until the code mentions a PostgreSQL type. Then the compiler stops:
# Wrong - the postgres module does not exist without the feature
sqlx = { version = "0.9", features = ["runtime-tokio"] }
use sqlx::postgres::{PgPool, PgPoolOptions};
error[E0432]: unresolved import `sqlx::postgres`
The error names the module, not the feature, which is why beginners misread it as a wrong import. The import is correct. The module is absent because the feature that builds it is off. The fix is one word in Cargo.toml, not a rewrite of the import.
A second failure looks different. With the feature off, the URL scheme postgres:// is unknown to the crate, so a program that tries to connect from a string reports that no driver was found for the scheme. Both errors have the same single-word fix.
An Unresolved Import Means Check Cargo.toml:
When the compiler reports unresolved import sqlx::postgres, do not rewrite the use line. Open Cargo.toml and confirm "postgres" is in the features list. This is the most common SQLx setup error, and it is always fixed in the manifest, never in the code.
Feature Unification Across the Project
Cargo builds each crate once per project, with the union of every feature any dependency asks for. This is called feature unification, and it has one practical consequence: if two crates in the same project both depend on SQLx, and one asks for postgres while the other asks for sqlite, the build includes both drivers. A beginner meets this when adding a second crate, such as SeaORM, which depends on SQLx itself. The pool types still come from the features this project lists, and the extra driver costs compile time but changes no behaviour.
The reverse is also worth knowing: there is no way to turn a feature off for one dependency and on for another. Features are per crate, per whole build. That is why the dependency line is written once, in the project's own Cargo.toml, and every other crate shares it.
The Full Dependency Line in Context
Here is the complete dependency block this cluster uses, with the feature line in place:
[package]
name = "axum-project"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.8"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { version = "0.9", features = ["postgres", "runtime-tokio", "macros", "derive"] }
[dev-dependencies]
tower = { version = "0.5", features = ["util"] }
Run cargo check after any edit to this file. A clean check confirms the feature set, and a feature error at this stage is cheap: it is found before any database is involved, before any handler runs, and before any query is written.
Summary
The postgres feature is a small switch with a large effect: one word decides whether the entire PostgreSQL side of SQLx exists. The insight worth keeping is that SQLx is a shell until a driver feature fills it. The crate provides the machinery for pools, queries, and migrations, and the feature provides the language that machinery speaks.
That split is what makes the next question answerable on its own: the crate knows PostgreSQL now, but it still needs an async runtime to do anything with that knowledge. The runtime is a separate choice, a separate feature, and it decides how the driver's network work actually runs.