Python 和 Postgresql:OperationalError:fe_sendauth:未提供密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33928585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:05:45  来源:igfitidea点击:

Python and Postgresql: OperationalError: fe_sendauth: no password supplied

pythonpostgresqlpeewee

提问by Becky S

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. This is all being run in an Ubuntu Vagrant Virtual Machine.

我正在开发一个使用 Peewee 和 Psycopg2 访问 PostGresQL 数据库的 Python 应用程序。这一切都在 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.conffile 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:

这是我的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.

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

回答by Bogdan Ruzhitskiy

In pg_hba.conf file use trustaccess method for our fe_sendauth user

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

回答by sl mahis

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 服务器密码