From d8d45b6e8c4f18a8b61142a93a5d63d1710ce4fc Mon Sep 17 00:00:00 2001 From: Bailey Hayes Date: Wed, 18 Feb 2026 10:03:27 -0500 Subject: [PATCH] Make `whoami` an optional feature for postgres Make the `whoami` dependency a feature gate (`whoami`) on `sqlx-postgres` so it can be disabled by users who don't need OS username fallback. When disabled, the default username falls back to "unknown" if `PGUSER` is not set. The feature is included in the top-level `sqlx` crate's default features, so existing behavior is preserved. Follows up on #1571 and #2319. --- Cargo.toml | 3 ++- sqlx-postgres/Cargo.toml | 3 ++- sqlx-postgres/src/options/mod.rs | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c88ab231e2..108850b230 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,7 @@ features = ["_unstable-docs"] rustdoc-args = ["--cfg", "docsrs"] [features] -default = ["any", "macros", "migrate", "json"] +default = ["any", "macros", "migrate", "json", "whoami"] derive = ["sqlx-macros/derive"] macros = ["derive", "sqlx-macros/macros", "sqlx-core/offline", "sqlx-mysql?/offline", "sqlx-postgres?/offline", "sqlx-sqlite?/offline"] @@ -159,6 +159,7 @@ rust_decimal = ["sqlx-core/rust_decimal", "sqlx-macros?/rust_decimal", "sqlx-mys time = ["sqlx-core/time", "sqlx-macros?/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"] uuid = ["sqlx-core/uuid", "sqlx-macros?/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"] regexp = ["sqlx-sqlite?/regexp"] +whoami = ["sqlx-postgres?/whoami"] bstr = ["sqlx-core/bstr"] [workspace.dependencies] diff --git a/sqlx-postgres/Cargo.toml b/sqlx-postgres/Cargo.toml index 1ab7c713ad..8c4888942c 100644 --- a/sqlx-postgres/Cargo.toml +++ b/sqlx-postgres/Cargo.toml @@ -14,6 +14,7 @@ any = ["sqlx-core/any"] json = ["dep:serde", "dep:serde_json", "sqlx-core/json"] migrate = ["sqlx-core/migrate"] offline = ["json", "sqlx-core/offline", "smallvec/serde"] +whoami = ["dep:whoami"] # Type Integration features bigdecimal = ["dep:bigdecimal", "dep:num-bigint", "sqlx-core/bigdecimal"] @@ -65,7 +66,7 @@ num-bigint = { version = "0.4.3", optional = true } smallvec = { version = "1.7.0" } stringprep = "0.1.2" tracing = { version = "0.1.37", features = ["log"] } -whoami = { version = "2.0.2", default-features = false } +whoami = { version = "2.0.2", default-features = false, optional = true } dotenvy.workspace = true thiserror.workspace = true diff --git a/sqlx-postgres/src/options/mod.rs b/sqlx-postgres/src/options/mod.rs index 21e6628cae..3564f33af3 100644 --- a/sqlx-postgres/src/options/mod.rs +++ b/sqlx-postgres/src/options/mod.rs @@ -64,14 +64,7 @@ impl PgConnectOptions { .or_else(|| var("PGHOST").ok()) .unwrap_or_else(|| default_host(port)); - let username = if let Ok(username) = var("PGUSER") { - username - } else if let Ok(username) = whoami::username() { - username - } else { - // keep the same fallback as previous version - "unknown".to_string() - }; + let username = var("PGUSER").ok().unwrap_or_else(default_username); let database = var("PGDATABASE").ok(); @@ -582,6 +575,16 @@ impl PgConnectOptions { } } +fn default_username() -> String { + #[cfg(feature = "whoami")] + if let Ok(username) = whoami::username() { + return username; + } + + // keep the same fallback as previous version + "unknown".to_string() +} + fn default_host(port: u16) -> String { // try to check for the existence of a unix socket and uses that let socket = format!(".s.PGSQL.{port}");