postgres crypto sha256 - ghdrako/doc_snipets GitHub Wiki

=> CREATE EXTENSION pgcrypto;
=> SELECT digest('blah', 'sha256');

Starting from PostgreSQL 11 you could use built-in functions to calculate hash value:

SELECT sha256('hello world!');

SELECT encode(sha256('hello world!'),'base64');

Starting with PostgreSQL 12 you can use a generated column; it could be as simple as:

CREATE TABLE codes (
    ...,
    key_code text,
    sha_code text GENERATED ALWAYS AS (encode(sha256(key_code::bytea), 'hex')) STORED
);