[PATCH] initdb: Treat empty -U argument as unset username - Mailing list pgsql-hackers

From Jianghua Yang
Subject [PATCH] initdb: Treat empty -U argument as unset username
Date
Msg-id CAAZLFmRK+XFp=mqCeruyNVkqGq5mH45CP+e-8oNttPRtLuB5eQ@mail.gmail.com
Whole thread Raw
Responses Re: [PATCH] initdb: Treat empty -U argument as unset username
List pgsql-hackers
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  

Attachment

pgsql-hackers by date:

Previous
From: Steve Chavez
Date:
Subject: PG 18 beta1 release notes misses mention of pg_noreturn
Next
From: wenhui qiu
Date:
Subject: Re: Making pg_rewind faster