59

This is probably a silly error but I cannot seem to find a satisfying solution.

When running db.create_all(), I got the following error.

sqlalchemy.exc.OperationalError: (OperationalError) fe_sendauth: no password supplied  None None

My database link is set as

'postgresql://localhost/db_name'

This worked fine on my Mac and Heroku, but is not OK on ubuntu (digitalocean).

Any ideas what I might be doing wrong?

6 Answers 6

95

You probably just need to remove "localhost" from your connection string:

'postgresql:///db_name'

That tells psycopg2 to use Unix-domain sockets. Your default configuration will use "ident" so you'll be connecting as the user that runs the script. In the default configuration, "md5" only applies to TCP connections.

4
  • 7
    And how would you pass a different username in this way? Commented Apr 14, 2018 at 23:25
  • "Note: When ident is specified for a local (non-TCP/IP) connection, peer authentication (see Section 20.3.6) will be used instead." - postgresql.org/docs/9.6/static/auth-methods.html#AUTH-PEER - use "map" configuration to map os username to db username. Commented May 19, 2018 at 8:33
  • I am doing this on windows and I get the same error despite having the correct db string. Commented Feb 2, 2021 at 2:08
  • 1
    How do you know it's correct if it's not working? Commented Aug 24, 2021 at 15:13
51

URL pattern should be:

postgresql://user:password@localhost:5432/database_name

pip install psycopg2
the user should be postgres or any other user you have created and intend to use

similarly for mySql it would be:

mysql://user:pass@localhost:3306/database_name

pip install mysql-python

7
  • 5
    I'm using peer authentication (so no password) and this isn't working for me, getting no password supplied. My connection string is postgresql://user@localhost/db where user is the linux user I'm running the code as.
    – lfk
    Commented Jun 4, 2018 at 6:50
  • 2
    I used this solution to solve my problem but in my case but I omitted the port from the URL: postgresql://user:password@localhost/database_name.
    – khwilo
    Commented Apr 22, 2019 at 9:59
  • Thx @khwilo For users of AWS RDS, the default database_name is postgres
    – FarNorth
    Commented Feb 1, 2021 at 22:18
  • 1
    @omokehinde igbekoyi is there any way to hide the user and pass ? I have a script which has command same like you but if somebody grep for the PostgreSQL process, it give a user id and password too. I dont want to expose that.
    – punam
    Commented Jan 11, 2022 at 9:56
  • 1
    @punam you could set these in your env variable instead Commented Jan 13, 2022 at 10:56
9

On your Mac, PostgreSQL was set up for trust or peer authentication for connections from localhost.

On your Ubuntu box it's set up for md5 authentication for connections from localhost.

You'll want to configure a password, or change the authentication mode. See pg_hba.conf, and the Ubuntu guide for PostgreSQL (there's a section about this error).

4

Below worked for me. Your connection to your postgres database requires a password; thus, below is what you should write..

pg_user = "magicmike"
pg_pwd = "test123"
pg_port = "5432"
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://{username}:{password}@localhost:{port}/foodversity_db".format(username=pg_user, password=pg_pwd, port=pg_port)
1
  • is there any way to hide the username and password ? I have a script which has command to connect PostgreSQL but if somebody grep ps -ef | postgresql , it give a user id and password too. I dont want to expose that
    – punam
    Commented Jan 11, 2022 at 9:57
-1

First make sure that the database server is connected and then run the command again.Silly, but it worked for me.

-2

For Remote Server

  • remote server => postgresql://<username>:<password>@<ipaddress>:<port>/<database>

For Local in configuration use

  • local db => postgressql:///<database>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.