Python和Postgresql:OperationalError:fe_sendauth:未提供密码
作者:互联网
我知道在StackOverflow上有很多类似的问题,但是我已经阅读并重新阅读了它们,但似乎无法解决我的特定问题.
我正在开发一个使用Peewee和Psycopg2访问PostGresQL数据库的Python应用程序.所有这些都在Ubuntu Vagrant虚拟机中运行.
当我尝试通过Python添加用户时,我一直收到此错误:
peewee.OperationalError:fe_sendauth:未提供密码
这是我尝试添加用户的代码:
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
我已经将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
并重新启动了我的postgresql服务器.
这是我的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
请您给予的任何帮助,我们将不胜感激!
注意:最奇怪的部分是这一切似乎都在起作用,然后我不知道自己做了什么更改,但是它停止了工作.
解决方法:
在pg_hba.conf文件中,对我们的fe_sendauth用户使用信任访问方法
标签:peewee,postgresql,python 来源: https://codeday.me/bug/20191027/1946061.html