Can not CREATE USER in postgresql

896 Views Asked by At

I tried on powershell, command prompt, and ubuntu for windows:

postgres=# CREATE USER sally WITH PASSWORD 'secret';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 sally     |                                                            | {}
postgres=# \q
> psql -U sally
Password for user sally:
psql: error: could not connect to server: FATAL:  password authentication failed for user "sally"

Also if I write psql or $ sudo su - postgres (in ubuntu)

It would ask me for my Windows USERNAME (respectfuly):

Password for user xyz:

[sudo] password for xyz:

Here's my pg_hba.conf

...
# local      DATABASE  USER  METHOD  [OPTIONS]
# host       DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostssl    DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostnossl  DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
#
...
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# 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.
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
1

There are 1 best solutions below

3
On

Either add -h 127.0.0.1 when connecting, or add a local rule to pg_hba.conf.

Also, specify the wanted DB name if it's not the same as the user name: -d <dbname>.