public inbox for [email protected]  
help / color / mirror / Atom feed
From: Jianghua Yang <[email protected]>
To: [email protected]
Subject: [PATCH] initdb: Treat empty -U argument as unset username
Date: Tue, 1 Jul 2025 19:55:49 -0700
Message-ID: <CAAZLFmRK+XFp=mqCeruyNVkqGq5mH45CP+e-8oNttPRtLuB5eQ@mail.gmail.com> (raw)

Hi hackers,

While working with `initdb`, I noticed that passing an empty string to the
`-U` option (e.g., `initdb -U ''`) causes it to fail with a misleading
error:


performing post-bootstrap initialization ... 2025-07-01 19:48:42.006 PDT
[14888] FATAL:  role """ does not exist at character 72

2025-07-01 19:48:42.006 PDT [14888] STATEMENT:

UPDATE pg_class   SET relacl = (SELECT array_agg(a.acl) FROM  (SELECT
E'=r/""' as acl   UNION SELECT unnest(pg_catalog.acldefault(    CASE WHEN
relkind = 'S' THEN 's'          ELSE 'r' END::"char",10::oid)) ) as a)   WHERE
relkind IN ('r', 'v', 'm', 'S')  AND relacl IS NULL;

This happens because `initdb` accepts the empty string as a valid role name
and attempts to use it as the database superuser, which is not intended and
fails during bootstrap SQL.

I propose a small patch that treats an empty string passed to `-U` as if
the option was not provided at all — falling back to the current system
user, which is the documented and expected behavior when `-U` is omitted.

This change improves robustness and avoids confusing failure messages due
to user input that is technically invalid but easy to produce (e.g., via
scripting or argument quoting issues).

### Patch summary:

- Checks if the passed `username` is non-null but empty (`'\0'`)
- Replaces it with the effective system user in that case
- Keeps the logic consistent with the existing behavior when `-U` is omitted

Let me know if this approach seems reasonable or if you’d prefer we
explicitly reject empty usernames with an error instead.

Patch attached.

Best regards,
Jianghua Yang


Attachments:

  [application/octet-stream] 0001-initdb-Treat-empty-U-argument-as-unset-username.patch (728B, 3-0001-initdb-Treat-empty-U-argument-as-unset-username.patch)
  download | inline diff:
From 38c34daaad05825d41027b8f5962658b3eee507a Mon Sep 17 00:00:00 2001
From: Jianghua Yang <[email protected]>
Date: Tue, 1 Jul 2025 19:52:52 -0700
Subject: [PATCH] initdb: Treat empty -U argument as unset username

---
 src/bin/initdb/initdb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 62bbd08d9f6..2df15996271 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -3472,7 +3472,7 @@ main(int argc, char *argv[])
 	setup_bin_paths(argv[0]);
 
 	effective_user = get_id();
-	if (!username)
+	if (!username || username[0] == '\0')
 		username = effective_user;
 
 	if (strncmp(username, "pg_", 3) == 0)
-- 
2.39.5 (Apple Git-154)



view thread (16+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: [PATCH] initdb: Treat empty -U argument as unset username
  In-Reply-To: <CAAZLFmRK+XFp=mqCeruyNVkqGq5mH45CP+e-8oNttPRtLuB5eQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox