'bcrypt and postgresql. What data type should be used?

On flask-bcrypt extension, the encrypted string is stored as bytes object like this (with python 3.5):

>>> user.password
b'$2b$12$3UutBDuGIrxp2z95alVTp.0HO3qQEtk7O/emR0UC27aNaJKC/WCU.'

But, when that string stored on postgresql it's converted and become like this:

>>> user.password
'\\x243262243132244a546d7673453238354c754a497a4a334f37644a307559672f52796a486a526c4f443431536f387748544132303077447176555675'

Of course, it won't pass flask-bcrypt check password. I tried to encode the password before save it to database and whenever check password is called I decode it back. And it works.

My question is, what data type should I use? Am I have to use BLOB data type on postgresql? Or is what I've done above (encode and decode) the right thing to do? I don't have this issue when using sqlite3.



Solution 1:[1]

you need to convert the bytes to a python string (as all the bytes are ascii characters) then you can store it in a text (or other character type) column

Solution 2:[2]

I had a similar problem and I solved it using BYTEA data type at the Postgresql. I think problem occurs while writing byte array into VARCHAR datatype in Postgre. This might be related to database encoding and the details of that conversion from byte to varchar.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 YnScN