Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Role" does not exists in Postgresql

Tags:

postgresql

I have been reading this tutorial. There is the below command to login as a user:

# login as user "user"
psql -U user

when I execute the above command, I get an error saying the following:

psql: FATAL:  role "user" does not exist

The only help I saw online was here, the have said the following:

FATAL: role "myusername" does not exist

By default PostgreSQL connects to the PostgreSQL user with the same name as the current unix user. You have not created a PostgreSQL user by that name in your database.

Create a suitable user, or specify a different username to connect with. In the command line tools the -U flag does this.

But I still don't quite understand why I am getting this error.

like image 302
Alexander Solonik Avatar asked Dec 05 '22 02:12

Alexander Solonik


2 Answers

You have first to login in a linux shell as the user postgres and than create new postgres user with the command createuser. The system user postgres is created automatically by the Postgres installer.

Execute this code in the console (change your_username with a username of your choice):

sudo -u postgres -i
createuser -l -d -P your_username

Better you create a database with the same name too (this makes the login later easier):

createdb your_username -O your_username

Then you should be able to connect in psql with

psql -h localhost -U your_username
like image 198
Tom-db Avatar answered Dec 06 '22 16:12

Tom-db


Please check to see if the user exists.

\dg

If not, you need what is missing in the tutorial and you edit it if necessary.

CREATE USER user WITH PASSWORD '<here you password>';
like image 30
René Avatar answered Dec 06 '22 16:12

René