Postgresql psql: error: FATAL: Peer authentication failed for user "userrole" - python

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"

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

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

I had the same problem and fixed it by using following command in Ubuntu:
psql -d database -U username -h 127.0.0.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

Related

Python psycopg2, NO postgresql etc service running on mashine... Where is the postgress driver? where is pg_hba.conf? (FATAL: no pg_hba.conf entry)

I am using -in Linux- python 3 and psycopg2 to connect to some postres databases:
import psycopg2 as pg
connection = None
try:
connection = pg.connect(
user = "username",
password = "...",
host = "host_ip",
port = "5432",
database = "db_name",
)
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
except (Exception, pg.Error) as error :
print ("Error while connecting to PostgreSQL", error)
For one of the DBs this works, but for the other I am getting:
Error while connecting to PostgreSQL FATAL: no pg_hba.conf entry for host "XXX.XXX.XXX.XXX", user "YYYY", database "ZZZZ", SSL off
I have checked the web and stackoverflow, and there are a lot of similar questions,
e.g. Psycopg2 reporting pg_hba.conf error
However, I am not root on the machine where I used pip/anaconda, and there seems to be no
sql service or anything similar running:
$ sudo systemctl status postgres*
$ sudo systemctl status postgres
Unit postgres.service could not be found.
$ sudo systemctl status postgresql
Unit postgresql.service could not be found.
$ sudo systemctl status post*
So none of the answers seem to be relevant, because this question seems to be based on the
postgress service running, or on the existence of pg_hba.conf, either of which do not in my system. Though note that a sample is included in my envs/py3/share/ (where py3 the name of my environment):
$ locate pg_hba.conf
/home/nick/anaconda3/envs/py3/share/pg_hba.conf.sample
My question here aims to -apart to find a way to solve my immediate problem- understand what psycopg2 is / how it ends up using pg_hba.conf, seemingly used in a postgresql service that does not seem to exist in my system:
Does psycopg2 is/uses a driver? Why does it seem to include pg_hba.conf.sample and what is one supposed to do with it? where to place pg_hba.conf(???) it to make psycopg2 read it?
Notes / Info based on comments:
The DB is not locally hosted. It is running on a different server.
I am able to access that DB using DBeaver and my local Ubuntu python, but a container (same psycopg2 version is not), so I speculate it is not a DB server issue.
It seems pg_hba.conf is a file that should only be on the server? (If so, that actually is part of the answer I am looking for...)

Cannot re-execute code until manually shutdown local PostgreSQL server

I have code which activates my local Postgres server if it is not currently on, but once this command is sent, then I am unable to re-run anything in my editor. VSCode simply tells me "Code is currently running!" and the Output indicates that it is waiting for the server to disconnect before actually completing the entire script.
I want to be able to connect to postgresql straight-away by using psycopg2 and avoiding having to handle starting / stopping the local server, just as I would be able to with the EnterpriseDB installer version of PostgreSQL. However, if I can start the server, query the database, and then go about my merry way, that would also solve my issue. I want to be able to work on this Python script and others without locking up VSCode.
My issue stems from having to find a work-around for installing PostgreSQL on Windows 10. The installer was leading to a false "COMSPEC" environment variable error, so I unpacked the binaries instead. Unfortunately, I think that there is some issue with the configuration, since I am not able to run a simple query like the one below, which means that Postgres doesn't automatically start when called with psycopg2 in Python :
import psycopg2
conn = psycopg2.connect(
user='postgres',
host='127.0.0.1',
port='5432',
database='postgres'
)
cursor = conn.cursor()
SQL = 'select * from dual'
records = cursor.fetchall()
for record in records:
print('dummy :', record[0],'\n')
cursor.close()
conn.close()
^^^ This will return the following error, which is fixed when I start the server with pg_ctl :
Traceback (most recent call last):
File "c:\Users\UserName\Desktop\Test.py", line 7, in <module>
database='postgres'
File "C:\Users\UserName\AppData\Local\Programs\Python\Python37\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
I have manually gone into my command prompt and run these :
pg_ctl -D "C:\Program Files\pgsql\data" stop
pg_ctl -D "C:\Program Files\pgsql\data" start
Ideally, I would be able to have this handled automatically, i.e. I can run a script and not need to shut off the server in order to re-run. Ideally, the server could get started in a background process which is separate from the script's process.
import os
import psycopg2
import subprocess
pg_ctl = r'C:\Program Files\pgsql\bin\pg_ctl.exe'
data_dir = r'C:\Program Files\pgsql\data'
def server_status(exe,data):
exe=exe
data=data
if (os.path.isfile(exe)) and (os.path.isdir(data)) :
proc = subprocess.Popen([exe,'-D',data,'status'],stdout = subprocess.PIPE)
server_status = proc.communicate()[0].rstrip().decode("utf-8")
elif (os.path.isfile(exe)) and not (os.path.isdir(data)) :
server_status = f'PostgreSQL data does not exist here : \n {data}'
elif not (os.path.isfile(exe)) and (os.path.isdir(data)) :
server_status = f'PostgreSQL Executable "pg_ctl.exe" does not exist here : \n {os.path.dirname(exe)}'
else :
server_status = 'Input parameters cannot be executed.\nPlease check where "pg_ctl.exe" and the database reside'
return server_status
def server_on(exe,data):
exe=exe
data=data
if server_status(exe,data) == 'pg_ctl: no server running':
try:
subprocess.check_call([exe,'-D',data,'start'])
return 'server started'
except (subprocess.CalledProcessError) as ex:
return f'Failed to invoke psql: {ex}'
elif server_status(exe,data) == 'server started':
return 'server started already'
print(server_status(pg_ctl,data_dir))
server_on(pg_ctl,data_dir)
print(server_status(pg_ctl,data_dir))
If the server is off, I get : 'server started' returned as the server_status. Then I cannot run anything until I manually shutdown the server. "Code is currently running!" is what is returned (by VSCode) once I try to edit the code and re-run immediately afterwards.
Install PostgreSQL with Binaries :
Download PostgrSQL Binaries
Unzip the downloaded file in the location that you want to have as your base directory for PostgreSQL
Open your CMD prompt, navigate to your "bin" e.g. "C:\Program Files\pgsql\bin"
Initialize the database : initdb [option...] [ --pgdata | -D ] directory
E.g. : initdb.exe -D ../data --username=postgres --auth=trust
^^^ This will create the directory "data" in the same directory as "bin" then create a username "postgres". Note, no password specified here. Only the directory is a required argument
Start the server : pg_ctl [option...] [ --pgdata | -D ] directory
E.g. pg_ctl.exe start -D ../data
^^^ This will start the server with what was initialized in the "\data" directory
Connect to "postgres" now that the server is up : psql --username=postgres
Execute : ALTER USER postgres WITH PASSWORD "my_password"
Execute : CREATE EXTENSION adminpack;
Connect to a database : psql DBNAME USERNAME
Switch databases : \c DBNAME
Exit : \q
Show all active connections in the CMD prompt : netstat -nat
edit "postgresql.conf" file as needed (within your "\data" directory) --> E.g. "listen_addresses = 'localhost'" and "port = 5432"
Register PostgreSQL as a service : pg_ctl register [-D datadir] [-N servicename] [-U username] [-P password] [-S a[uto] | d[emand] ] [-e source] [-W] [-t seconds] [-s] [-o options]
Links :
PostgreSQL Documentation
PostgreSQL 11 initdb.exe
PostgreSQL 11 pg_ctl.exe
PostgreSQL 11 start the server
Install PostgreSQL Binaries (Windows 10)
Install PostgreSQL Binaries and Register It as a Service
Enable Remote PostgreSQL Connection
Configure PostgreSQL to Allow Remote Connection
Allow Remote Connections
Accept TCPIP Connections
Configure PostgreSQL to Accept Local Connections Only
PostgreSQL Management on Windows
Starting PostgreSQL in Windows w/o Install
StackOverflow :
unix_socket_directories
PostgreSQL Database Service
How to use PostgreSQL in multi thread python program
How to run PostgreSQL as a service in windows?
Register and run PostgreSQL as Windows Service
PostgreSQL pg_ctl Register Service Error under Windows
How can I configure PostgreSQL to start automatically in Windows?
PostgreSQL isn't Listening on Port 5432 in Windows
PostgreSQL initialization on Linux
Update :
I have tried to register PostgreSQL as a service, but I do not have admin privileges. I believe this is the root of my problem, since I only get the error "pg_ctl: could not open service manager" when I try to execute :
pg_ctl.exe register -N postgres -D "C:\Program Files\pgsql\data"
I would either need to disable to firewall or have a batch file kick-off a command to start the PostgreSQL server on a separate thread to my Python scripts. Or I could just switch to Linux and literally none of this would be an issue :D

MySQLdb: connect to MariaDB via socket

I've got a fresh MariaDB installation, without password.
Connecting via mysql works for the root user, without password and without any additional parameters.
$ sudo -i root
$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Server version: 10.2.21-MariaDB-log MariaDB Server
But connecting to socket using python MySQLdb library fails:
$ python
Python 2.7.5 (default, Sep 12 2018, 05:31:16)
>>> import MySQLdb
>>> MySQLdb.connect(unix_socket='/var/lib/mysql/mysql.sock')
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
Is it a problem for MariaDB vs. MySQL compatibility? Can anybody reproduce it?
It was not a passwordless root account. It was one with a default password.
mysql just read it automatically from /etc/my.cnf.d/ folder
[client]
socket = /var/lib/mysql/mysql.sock
host = localhost
user = root
password = ...
To debug mysql CLI-behaviour vs. python MySQLdb behaviour, you can run
mysql --no-defaults
which already reproduced the behaviour for me, so I continued with:
mysql --no-defaults --socket=/var/lib/mysql/mysql.sock
mysql --no-defaults --socket=/var/lib/mysql/mysql.sock --user=root
then got the idea to dig deeper into /etc/my.cnf.d/ folder.

MySQL: django.db.utils.OperationalError: (1698, "Access denied for user 'root'#'localhost'") with correct username and pw

I have django.db.utils.OperationalError: (1698, "Access denied for user 'root'#'localhost'") when using mysql. The username and pw are correct:
DB_HOST = '127.0.0.1'
DB_USER = 'root'
DB_PASSWORD = ''
I can log into mysql as root:
$ sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
But not as cchilders:
$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
This may contribute to the problem. Last time I installed mysql this didn't happen, so it doesn't make sense to me. I have the client fine:
$ pip3 freeze
Django==1.10.5
mysqlclient==1.3.9
How can I allow mysql to be run by my normal user, so I can run django in the terminal? thank you
Dirty solution:
Without any fixes, always run mysql as sudo
The reason that you can login as root on your server is that you probably have specified a password in the .my.cnf file in /root (i.e., the root user's home directory). Check to see if there is a password there and use that for cchilders as well. You can then create a django-specific application user to make sure that the django app only reads/writes/etc. to the databases that it needs access to and not access through the root mysql user.
create user 'django'#'localhost' identified by 'django-user-password';
grant usage on *.* to 'django'#'localhost';
grant all privileges on django-database-1.* to 'django'#'localhost';
Create a non-root SQL user and change the DB_USER variable in the settings.py file of Django

Access remote database failed,"(1045,Access denied for user......)"

local machine environment:
centos 6.7
python 2.7.10
Flask-MySQLdb 0.2.0
local ip: 112.25.76.72
remote ip: 221.228.203.203
Both of the code run on local Machine;
when I access the remote database in local python command line environment like this:
>>>import MySQLdb
>>>conn = MySQLdb.connect(host="221.228.203.203",user="test_user",passwd="test_passwd",db="test_db",charset="utf-8")
it access successfully,but when I write these code into a python script login.py:
import MySQLdb
conn = None
try:
conn = MySQLdb.connect(host="221.228.203.203",user="test_user",passwd="test_passwd",db="test_db",charset="utf-8")
cur = conn.cursor()
except Exception,e:
print e
finally:
if conn:
conn.close()
then execute "python login.py" in the terminal,it access failed,the error info is :
(1045, "Access denied for user 'test_user'#'112.25.76.72' (using password: YES)")
Why? Their python environment is the same.
if you can solve it,please help me.
You should check the configurations of your user in the database server. It will be stored in database mysql.
You can get the list of users with:
SELECT user, host FROM mysql.user;
You shouldn't delete the user test_user#localhost. You can try to execute it on your database server
CREATE USER 'test_user'#'%' IDENTIFIED BY 'test_passwd'
GRANT ALL PRIVILEGES ON *.* TO 'test_user'#'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'test_user'#'localhost' WITH GRANT OPTION;

Categories