title: Postgresql/psycopg2 password authentication error even though user and password are correct
body: <p>I am new to web development in Python and would really appreciate some help. I am trying to set up psycopg2/peewee on WSL (Windows machine). In my Python code I store all of the info I'll need to access a Postgres database, and then attempt to connect to the database as follows:</p>
  <pre><code>#lib/main.py
  from peewee import *
  db = PostgresqlDatabase('people', user='postgres', password='postgres', host='localhost', port=5432)
  db.connect()
  </code></pre>
  <p>I am certain that I have a Postgres user named 'postgres' with a password of 'postgres'. Those are the credentials I use to start up a Postgres server once I have changed into the postgres user on my machine using <code>su - postgres</code>.</p>
  <p>When I launch my Python virtual environment using <code>pipenv shell</code> and then run <code>python3 main.py</code> I get the following error:</p>
  <pre><code>// ♥  python3 main.py
  Traceback (most recent call last):
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 3035, in connect
      self._state.set_connection(self._connect())
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 3730, in _connect
      conn = psycopg2.connect(database=self.database, **self.connect_params)
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  **psycopg2.OperationalError: FATAL:  password authentication failed for user &quot;postgres&quot;**  
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;main.py&quot;, line 18, in &lt;module&gt;
      db.connect()
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 3038, in connect
      self._initialize_connection(self._state.conn)
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 2873, in __exit__
      reraise(new_type, new_type(exc_value, *exc_args), traceback)
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 183, in reraise
      raise value.with_traceback(tb)
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 3035, in connect
      self._state.set_connection(self._connect())
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/peewee.py&quot;, line 3730, in _connect
      conn = psycopg2.connect(database=self.database, **self.connect_params)
    File &quot;/home/allison_johnson/.local/share/virtualenvs/lib-jEOreobP/lib/python3.6/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  peewee.OperationalError: FATAL:  password authentication failed for user &quot;postgres&quot; 
  </code></pre>
  <p>I am confused as to why the authentication is failing for a valid user/password combo!</p>
  <p>I have tried testing the connection string in the Python3 shell as well:</p>
  <pre><code>&gt;&gt;&gt;import psycopg2
  &gt;&gt;&gt;psycopg2.connect(&quot;dbname=postgres user=postgres host=localhost password=postgres port=5432&quot;)
  </code></pre>
  <p>and I get the same error. I would much appreciate any insight as to why I can't connect my Python code to Postgres!</p>
  <p>My pg_hba.conf file has the following:</p>
  <pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # &quot;local&quot; is for Unix domain socket connections only
  local   all             all                                     peer
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            md5                                  # IPv6 local connections:
  host    all             all             ::1/128                 md5
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  local   replication     all                                     peer
  host    replication     all             127.0.0.1/32            md5
  host    replication     all             ::1/128                 md5
  </code></pre>
  <p>From the command line, <code>ps ax |grep postgres</code> shows:</p>
  <pre><code> 3218 ?        S      0:00 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf
   3220 ?        Ss     0:00 postgres: 10/main: checkpointer process
   3221 ?        Ss     0:00 postgres: 10/main: writer process
   3222 ?        Ss     0:00 postgres: 10/main: wal writer process
   3223 ?        Ss     0:00 postgres: 10/main: autovacuum launcher process
   3224 ?        Ss     0:00 postgres: 10/main: stats collector process
   3225 ?        Ss     0:00 postgres: 10/main: bgworker: logical replication launcher
   3337 tty1     S      0:00 grep --color=auto postgres
  </code></pre>
  <p>After running <code>psql -d postgres -U postgres -h 127.0.0.1</code> and inspecting the Postgres log, I see:</p>
  <pre><code>2020-08-12 13:37:30.573 EDT [709] LOG:  database system is shut down
  2020-08-12 13:37:30.700 EDT [3218] LOG:  listening on IPv4 address &quot;127.0.0.1&quot;, port 5433                                                              2020-08-12 13:37:30.714 EDT [3218] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5433&quot;
  2020-08-12 13:37:30.760 EDT [3219] LOG:  database system was shut down at 2020-08-12 13:37:30 EDT
  2020-08-12 13:37:30.816 EDT [3218] LOG:  database system is ready to accept connections
  2020-08-12 13:37:31.383 EDT [3226] [unknown]@[unknown] LOG:  incomplete startup packet
  2020-08-12 13:37:48.061 EDT [3254] postgres@postgres LOG:  provided user name (postgres) and authenticated user name (allison_johnson) do not match
  2020-08-12 13:37:48.061 EDT [3254] postgres@postgres FATAL:  Peer authentication failed for user &quot;postgres&quot;
  2020-08-12 13:37:48.061 EDT [3254] postgres@postgres DETAIL:  Connection matched pg_hba.conf line 85: &quot;local   all             postgres               $2020-08-12 13:42:30.909 EDT [3220] WARNING:  could not flush dirty data: Function not implemented                                                      2020-08-12 14:08:00.001 EDT [3443] allison_johnson@allison_johnson FATAL:  password authentication failed for user &quot;allison_johnson&quot;
  2020-08-12 14:08:00.001 EDT [3443] allison_johnson@allison_johnson DETAIL:  Password does not match for user &quot;allison_johnson&quot;.
          Connection matched pg_hba.conf line 90: &quot;local   all             all                                     md5&quot;
  2020-08-12 14:13:49.143 EDT [3514] postgres@postgres LOG:  provided user name (postgres) and authenticated user name (allison_johnson) do not match
  2020-08-12 14:13:49.143 EDT [3514] postgres@postgres FATAL:  Peer authentication failed for user &quot;postgres&quot;
  2020-08-12 14:13:49.143 EDT [3514] postgres@postgres DETAIL:  Connection matched pg_hba.conf line 85: &quot;local   all             postgres               $
  </code></pre>
  <p>Lines 85-90 of pg_hba.conf:</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
  </code></pre>
  <p>Settings for port and listen_addresses in /main/postgresql.conf</p>
  <pre><code>#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                          # comma-separated list of addresses;
                                          # defaults to 'localhost'; use '*' for all
                                          # (change requires restart)
  port = 5433                             # (change requires restart)
  </code></pre>