Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
c8c87c6b0f fix(sql): recognize PGUSERNAME environment variable
Add PGUSERNAME to the PostgreSQL username resolution logic, matching
the documented behavior. The precedence order is now: options.username,
options.user, PGUSERNAME, PG_USER, PGUSER, USER, then "postgres".

Fixes #26684

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 07:02:23 +00:00
2 changed files with 27 additions and 2 deletions

View File

@@ -692,7 +692,8 @@ function parseOptions(
break;
}
case "postgres": {
username ||= options.username || options.user || env.PG_USER || env.PGUSER || env.USER || "postgres";
username ||=
options.username || options.user || env.PGUSERNAME || env.PG_USER || env.PGUSER || env.USER || "postgres";
break;
}
}

View File

@@ -25,7 +25,7 @@ describe("SQL adapter environment variable precedence", () => {
'MARIADB_URL', 'MARIADBURL',
'TLS_MARIADB_DATABASE_URL',
'SQLITE_URL', 'SQLITEURL',
'PGHOST', 'PGUSER', 'PGPASSWORD', 'PGDATABASE', 'PGPORT',
'PGHOST', 'PGUSER', 'PGUSERNAME', 'PGPASSWORD', 'PGDATABASE', 'PGPORT',
'MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_DATABASE', 'MYSQL_PORT'
];
@@ -142,6 +142,30 @@ describe("SQL adapter environment variable precedence", () => {
expect(options.options.username).toBe("postgres-user");
});
test("PGUSERNAME should take precedence over PGUSER (issue #26684)", () => {
process.env.USER = "generic-user";
process.env.PGUSER = "pguser-fallback";
process.env.PGUSERNAME = "pgusername-primary";
const options = new SQL({
adapter: "postgres",
});
expect(options.options.username).toBe("pgusername-primary");
});
test("PGUSER should work when PGUSERNAME is not set", () => {
process.env.USER = "generic-user";
process.env.PGUSER = "pguser-value";
// PGUSERNAME is not set
const options = new SQL({
adapter: "postgres",
});
expect(options.options.username).toBe("pguser-value");
});
test("should infer mysql adapter from MYSQL_URL env var", () => {
process.env.MYSQL_URL = "mysql://user:pass@host:3306/db";