[英]Python and Postgresql: OperationalError: fe_sendauth: no password supplied
I know there are a lot of similar questions on StackOverflow, but I have read and re-read them, and I cannot seem to solve my particular issue.我知道 StackOverflow 上有很多类似的问题,但我已经阅读并重新阅读了它们,但我似乎无法解决我的特定问题。
I am developing a Python application that uses Peewee and Psycopg2 to access PostGresQL databases.我正在开发一个使用 Peewee 和 Psycopg2 访问 PostGresQL 数据库的 Python 应用程序。 This is all being run in an Ubuntu Vagrant Virtual Machine.这一切都在 Ubuntu Vagrant 虚拟机中运行。
I keep getting this error when I try to add a user via Python:当我尝试通过 Python 添加用户时,我不断收到此错误:
peewee.OperationalError: fe_sendauth: no password supplied peewee.OperationalError:fe_sendauth:未提供密码
Here is the code where I try to add a user :这是我尝试添加用户的代码:
def add_user(user, password):
"""
:param username:
:param password:
:return:
"""
try:
#add user to admin user table
#AdminUserModel.create(username=user, password=password)
# add user to psql
conn = connect_to_db('postgres', 'postgres')
cursor = conn.cursor()
add_query = "CREATE ROLE %s WITH CREATEDB LOGIN PASSWORD %s"
add_data = (AsIs(user), password)
cursor.execute(add_query, add_data)
except IntegrityError:
return False
return True
def connect_to_db(db_name, username):
conn = psycopg2.connect(dbname=db_name, user=username)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
return conn
I have changed my pg_hba.conf file to:我已将pg_hba.conf文件更改为:
# Database administrative login by Unix domain socket
local all postgres trust
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all vagrant 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all vagrant ::1/128 trust
host all all ::1/128 md5
And have restarted my postgresql server.并重新启动了我的 postgresql 服务器。
Here is my Vagrantfile :这是我的流浪文件:
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provider :virtualbox do |vb|
# use for debugging the vm
# vb.gui = true
# speed up networking
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y python-software-properties
sudo apt-get update -y
# sudo apt-get upgrade -y
sudo apt-get install -y postgresql postgresql-contrib postgresql-client libpq-dev
sudo sed -i "s/#listen_address.*/listen_addresses '*'/" /etc/postgresql/9.3/main/postgresql.conf
sudo cp /home/vagrant/cs419-project/vagrant_files/pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
sudo service postgresql restart
# createuser -U postgres -s vagrant
sudo -u postgres psql -c "CREATE ROLE vagrant SUPERUSER LOGIN PASSWORD 'vagrant'"
sudo su postgres -c "createdb -E UTF8 -T template0 --locale=en_US.utf8 -O vagrant cs419"
sudo apt-get install -y python-pip python-psycopg2
sudo pip install peewee
SHELL
config.vm.synced_folder ".", "/home/vagrant/cs419-project", id: "vagrant",
owner: "vagrant",
group: "admin",
mount_options: ["dmode=775,fmode=664"]
end
Please, any help you can give is much appreciated!拜托,非常感谢您提供的任何帮助!
NOTE: The weirdest part is this all seemed to be working before and then I don't know what I changed but it stopped working.注意:最奇怪的部分是这一切似乎以前都在工作,然后我不知道我改变了什么,但它停止工作了。
在pg_hba.conf文件中,为fe_sendauth用户使用trust
访问方法
Can you try adding in a password changing this 您可以尝试添加更改此密码的密码吗?
conn = psycopg2.connect(dbname=db_name, user=username)
to this? 这个?
conn = psycopg2.connect(dbname=db_name, user=username, password=password)
this worked for me using my password for the postgres server 这对我使用postgres服务器的密码
Old Host,老主人,
host all all 127.0.0.1/32 md5
host all all ::1/0 md5
Change this host in the file "/etc/postgresql/13/main/pg_hba.conf"在文件“/etc/postgresql/13/main/pg_hba.conf”中更改此主机
Solution:-解决方案:-
step1:
sudo vi /etc/postgresql/13/main/pg_hba.conf
step2:
New Host, just change the md5 to trust like given below,
host all all 127.0.0.1/32 trust
host all all ::1/0 trust
step3:
save the file.
This method works for me.这种方法对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.