I'm trying to use Postgresql encode() function and put some other functions as it's arguments. I get errors, and I can't understand why.
I am using Postgres 9.6.14 on Windows.
698d51a19d8a121ce581499d7b701668
select md5('111');
select encode('698d51a19d8a121ce581499d7b701668', 'base64');
"ERROR: function encode(text, unknown) does not exist"
select encode(md5('111'), 'base64');
select encode(concat('1', '11'), 'base64');
select md5(concat('1', '11'))
So what's the issue with requests number 3 and 4 and encode()
function overall?
Look at the definition of encode
:
\df encode
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+--------+------------------+---------------------+------
pg_catalog | encode | text | bytea, text | func
(1 row)
The first argument has to be bytea
, that is a binary byte string.
Your first two queries work because string literals are of type unknown
, which can be cast to bytea
implicitly.
In the queries that do not work, you are using the functions md5
and concat
, which both have text
as a result type. Now there is no implicit cast between text
and bytea
, hence the error message.
To make that work, you have to introduce an explicit type cast:
select encode(CAST(md5('111') AS bytea), 'base64');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With