How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

Viewed 6601

I'm having trouble using PostgreSQL. I have recently installed this version (13+223.pgdg20.04+1) of postgresql package in ubuntu 20.04.

I'm trying to run psql command, but I get the following error:

psql: error: FATAL: role "my_username" does not exist

I have tried to create a new user with createuser me, but I get the following error:

createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist

I have tried also forcing the postgres user with createuser me --username=postgres, but I get the following error:

createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"

How do I solve these problems to use PostgreSQL locally on my computer without these problems?

PD: I have reinstalled postgres and now I'm getting a different error while doing psql:

psql: error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

3 Answers

You need to provide username in the psql command using -U option.

psql -U postgres

Postgresql comes with a predefined superuser role called postgres. If you want to create more roles, you first have to connect as this initial role.

Peer authentication means (there are advanced possibilities, but those are not going to be used by default, while the simple method is the default for apt-installed PostgreSQL) that you have to be the OS user 'postgres' to connect as the database user 'postgres'. So you would do:

sudo -u postgres createuser me

You don't need to specify --username=postgres, since that is the default behavior anyway once you use sudo -u postgres

Alternatively, you could change your pg_hba.conf to use a different authentication method other than peer, if you want to.

Related