From sudo documentation:
-u user, --user=user
Run the command as a user other than the default target user (usually root).
From (PostgreSQL) initdb
documentation:
Creating a database cluster consists of creating the directories in which the database data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the template1 and postgres databases.
and
-U username --username=username
Selects the user name of the database superuser. This defaults to the name of the effective user running initdb. It is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating system user's name is different.
From psql
documentation you quote you can see that, besides options, you call it with either a database name, or a database name and user name
If you do not provide a database name it tries to connect to the database name being the name of the user currently running psql
, as explained in documentation:
The default user name is your operating-system user name, as is the default database name.
sudo -u postgres psql -c "\l"