2

Trying to run sha256 function

CREATE EXTENSION pgcrypto;
CREATE OR REPLACE FUNCTION sha256(bytea) returns text AS $$
    SELECT encode(digest($1, 'sha256'), 'hex')
$$ LANGUAGE SQL STRICT IMMUTABLE;

WITH
tab_email as        (SELECT '[email protected]'::text as email FROM tmp),
INSERT INTO users (email, password) VALUES ((SELECT email FROM tab_email), sha256('mypass'));

i got this error

ERROR: function sha256(text) does not exist

3
  • INSERT INTO users SELECT email, sha256('mypass') FROM tab_email; Commented Nov 24, 2015 at 20:30
  • @lad2025 That's better, but does not explain the error. Which is odd. Commented Nov 25, 2015 at 1:15
  • What does \df *.sha256 show? How about SELECT sha256('1') ? Commented Nov 25, 2015 at 1:15

3 Answers 3

8

It's because Postgres's built-in sha256 function takes a bytea argument:

citus=> \df+ sha256
                                                                               List of functions
   Schema   |  Name  | Result data type | Argument data types | Type | Volatility | Parallel |  Owner   | Security | Access privileges | Language | Source code  | Description
------------+--------+------------------+---------------------+------+------------+----------+----------+----------+-------------------+----------+--------------+--------------
 pg_catalog | sha256 | bytea            | bytea               | func | immutable  | safe     | postgres | invoker  |                   | internal | sha256_bytea | SHA-256 hash
(1 row)

So just cast to ::bytea first.

citus=> select encode(sha256('a'::bytea), 'hex');
                              encode
------------------------------------------------------------------
 ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

0

I ended up using

encode(digest(password, 'sha256'), 'hex') from tab_password)

Comments

-1

If you create this function in redshift, you will be able to your f_sha256

CREATE OR REPLACE FUNCTION f_sha256 (mes VARCHAR)
    returns VARCHAR
    STABLE AS $$
    import hashlib
    return hashlib.sha256(mes).hexdigest()
    $$ language plpythonu;

1 Comment

See "Explaining entirely code-based answers". While this might be technically correct it doesn't explain why it solves the problem or should be the selected answer. We should educate in addition to help solve the problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.