Python and Postgresql: OperationalError: fe_sendauth: no password supplied

做~自己de王妃 提交于 2019-12-22 09:47:34

问题


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.

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.

I keep getting this error when I try to add a user via Python:

peewee.OperationalError: fe_sendauth: no password supplied

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:

# 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.

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.


回答1:


In pg_hba.conf file use trust access method for our fe_sendauth user




回答2:


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



来源:https://stackoverflow.com/questions/33928585/python-and-postgresql-operationalerror-fe-sendauth-no-password-supplied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!