Python和Postgresql:OperationalError:fe_sendauth:没有提供密码

时间:2015-11-26 00:06:55

标签: python postgresql peewee

我知道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

请您提供任何帮助,我们非常感谢!

注意:最奇怪的部分是这一切似乎都在以前工作,然后我不知道我改变了什么,但它停止了工作。

2 个答案:

答案 0 :(得分:2)

在pg_hba.conf文件中,为我们的fe_sendauth用户使用trust访问方法

答案 1 :(得分:2)

您可以尝试添加更改密码吗

conn = psycopg2.connect(dbname=db_name, user=username)

对此?

conn = psycopg2.connect(dbname=db_name, user=username, password=password)

使用我的postgres服务器密码,这对我有用