Q:

How does user authentication work in Postgresql

How exactly do users/roles work in Postgresql for access control and how are they different from users providing access control in the OS.
When installing Postgresql I can see that the user postgres is created as seen in /etc/passwd.
I can use psql through sudo and see the users/roles in Postgres.
$ sudo -u postgres psql
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}

This indicates that a role named postgres exists but attempts to use it instead of using sudo fail.
$ psql -U postgres
psql: FATAL:  Peer authentication failed for user "postgres"

Why, and what is the connection between the postgres user in /etc/passwd and the postgres role that is printed by \du

A:

The connection between the "postgres" OS user and the "postgres" role is:

They are spelled the same
If you don't specify -U option to psql, it defaults to using the operating system user name as the role name, which might be "postgres" if that is your OS user.
If you specify "peer" authorization in pg_hba.conf, which you seem to have done, then you have to be that os user to log in as that role.  If you don't want that rule, change it to something else, like trust, md5, etc.

