Flask & Alchemy - (psycopg2.OperationalError) FATAL: password authentication failed Flask & Alchemy - (psycopg2.OperationalError) FATAL: password authentication failed postgresql postgresql

Flask & Alchemy - (psycopg2.OperationalError) FATAL: password authentication failed


I was stuck with this same error. The problem for me was that I hadn't set the password for the psql user.See similar question with answer here:https://askubuntu.com/questions/413585/postgres-password-authentication-fails

it got solved when I did

ALTER USER db_username PASSWORD 'new_password'


its an old question and i guess its not important to you but for people with same problem in future:

i was stuck too. i found postgres default behavior converts everything to lowercase.[1]my problem solved when i converted my user to lowercase.

sorry for bad english :(


After some debugging of my sqlalchemy code, I saw that the url that sqlalchemy used was a decoded url-string (at least for postgres). This means that if you have substrings in your connection string such as %34, the sqlalchemy connection string will be 4, as that is the url-decoded string. The solution for this problem is simple: simply replace all occurences of % in the connection string with %25, as that is the url encoding for %. The code for this is simply:

from sqlalchemy import create_engineconnection_string_orig = "postgres://user_with_%34_in_the_string:pw@host:port/db"connection_string = connection_string_orig.replace("%", "%25")engine = create_engine(connection_string)print(engine.url) # should be identical to connection_string_origengine.connect()

This probably doesn't solve everyone's problem, but it's nevertheless good to be aware of it.