Search code examples
linuxdatabasepostgresqlpsqlpg-hba.conf

Linux (Fedora) Cannot run psql command in terminal - role does not exist


I have seen all the questions here on stackoverflow about similar topic, but I would like to know why this happens, where is the problem, and what to do to set it up correctly. I am learning to code, so I apologize for any misunderstandings. Please, have patience with me.

My case is -

I cannot run psql command from terminal.

Respond is -

psql: FATAL:  role "some_name" does not exist

after I write down and hit enter -

sudo -u postgres -i

everything just works and I can run psql command. I need to write sudo -u postgres -i command every time I open terminal, again and again.

I would like to kindly thanks to any respond.

If you got more questions I can give you more information.

Here see some additional info:

[postgres@localhost ~]$ psql
psql (9.6.6)
Type "help" for help.

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member
 of 
-----------+------------------------------------------------------------+-------
----
 matus     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Another:

[postgres@localhost ~]$ whoami
postgres

So, I have two opened terminals, one as postgres and another as user_name where I can do other stuff, using db somehow.


Solution

  • from your description I suppose you have peer authentication in hba file. To check it connect to db with psql (your sudo -u postgres method) and:

    t=# show hba_file ;
          hba_file
    ---------------------
     /pg/d10/pg_hba.conf
    (1 row)
    t=# \! grep local /pg/d10/pg_hba.conf | grep -v "#"
    local   all             all                                     peer
    

    if you see peer above trust, or just peer, than this is the case. from manuals:

    https://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-PEER

    The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping)

    formatting mine.

    so in order to avoid

    psql: FATAL: role "some_name" does not exist

    just create such db user:

    create user some_name;
    

    or change peer to trust in hba.conf