title: psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres" (Ubuntu)
body: <p>When I try to open <code>psql</code> with this command:</p>
  <pre><code>psql -U postgres
  </code></pre>
  <p>I get this error:</p>
  <pre><code>psql: error: connection to server on socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot; failed: FATAL:  Peer authentication failed for user &quot;postgres&quot;
  </code></pre>
  <p>But it connects successfully when I use:</p>
  <pre><code>sudo -u postgres psql
  </code></pre>
  <p>Can someone please explain what is happening and how to diagnose/fix this problem? My <code>pg_hba.conf</code> contains the following:</p>
  <pre><code>
  # Database administrative login by Unix domain socket
  local   all             postgres                                peer
  
  # TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # &quot;local&quot; is for Unix domain socket connections only
  local   all             all                                     md5
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            scram-sha-256
  # IPv6 local connections:
  host    all             all             ::1/128                 scram-sha-256
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  local   replication     all                                     peer
  </code></pre>