Python and Postgresql: OperationalError: fe_sendauth: no password supplied

Becky S picture Becky S · Nov 26, 2015 · Viewed 18.7k times · Source

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.

Answer

sl mahis picture sl mahis · Oct 11, 2018

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