5

I have installed PostgreSQL and created a user 'userrole' with superuser privileges. Also able to connect through python code.

import psycopg2
conn = psycopg2.connect(
   database="postgres", user="userrole", password="userroot", host='127.0.0.1', port= '5432'
)
cursor = conn.cursor()
cursor.execute("select version()")
data = cursor.fetchone()
print("Connection established to: ",data)
conn.close()

This is my output:

Connection established to:  ('PostgreSQL 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit',)

Process finished with exit code 0

The issue I'm facing is through CLI is cannot connect to this user:

(venv) resh@project:~/PycharmProjects/utilities$ sudo su -l userrole
[sudo] password for resh: 
su: user userrole does not exist

Sorry, I am new to ubuntu, now I have changed the command and still getting an issue :

(venv) resh@project:~/PycharmProjects/utilities$ sudo psql -U userrole
[sudo] password for resh: 
psql: error: FATAL:  Peer authentication failed for user "userrole"
3
  • A database user is different from an operating system user. su is about operating system users. Commented Apr 2, 2021 at 6:12
  • Now i am trying to access psql user and still facing the issue @LaurenzAlbe Commented Apr 2, 2021 at 6:20
  • Since I don't know what you are doing, I cannot say what you are doing wrong. Perhaps edit the question and add the new command. Commented Apr 2, 2021 at 6:30

5 Answers 5

13

Your latest psql attempt is trying to connect over the Unix socket, which is (apparently, based on error message) configured to user peer authentication.

To use the password, you need to connect over TCP, like your python is doing:

psql -U userrole -h 127.0.0.1

Also, the sudo is useless for password-based authentication, so I removed it

Sign up to request clarification or add additional context in comments.

Comments

2

I had the same problem and fixed it by using following command in Ubuntu:

psql -d database -U username -h 127.0.0.1

Comments

1

userrole is an SQL user. It is not a Linux user. The two user lists are completely separate.

Comments

0

Searched and scoured the web for a answer who will fix the problem. In my case it was solved simply by adding this line

TYPE DATABASE USER ADDRESS METHOD

local all youruser md5

Comments

-1
  • Open the pg_hba.conf file using any text editor. It is located at /etc/postgresql/[major.minor]/main/pg_hba.conf eg /etc/postgresql/12/main/pg_hba.conf location.

  • Change the following line

    local   all    postgres        peer
    

    to

    local   all    postgres        md5
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.