我知道在StackOverflow上有很多类似的问题,但我已经阅读并反复阅读了它们,似乎不能解决我的特定问题。
我正在开发一个Python应用程序,使用Peewee和Psycopg2来访问PostGresQL数据库。这一切都在Ubuntu Vagrant虚拟机中运行。
当我尝试通过Python添加用户时,我不断收到以下错误:
peewee.OperationalError: fe_sendauth: no password supplied
这是我尝试添加用户的代码:
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
任何您能提供的帮助都将不胜感激!
注意:最奇怪的部分是这似乎在之前都是有效的,但我不知道我改变了什么,它就停止工作了。