Skip to content

Commit 57f52ff

Browse files
Nilix007mehcode
authored andcommitted
postgres: Use $USER as default for the username
1 parent f3fe41e commit 57f52ff

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

sqlx-core/src/postgres/connection.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ pub struct PgConnection {
110110

111111
// https://www.postgresql.org/docs/12/protocol-flow.html#id-1.10.5.7.3
112112
async fn startup(stream: &mut PgStream, url: &Url) -> crate::Result<BackendKeyData> {
113-
// Defaults to postgres@.../postgres
114-
let username = url.username().unwrap_or(Cow::Borrowed("postgres"));
113+
// Defaults to $USER@.../$USER
114+
// and falls back to postgres@.../postgres
115+
let username = url
116+
.username()
117+
.or_else(|| std::env::var("USER").map(Cow::Owned).ok())
118+
.unwrap_or(Cow::Borrowed("postgres"));
115119
let database = url.database().unwrap_or(&username);
116120

117121
// See this doc for more runtime parameters

0 commit comments

Comments
 (0)