0

I want to encrypt a column using PostgreSQL. But I don't want the data in DB/Table to be encrypted. While querying/extracting the data from DB, I just want 'adsid' column to be encrypted in the output. Data in 'adsid' column is like a123456-111-dd43-123r-xdf1we3456z999. How can I do that?

3
  • 1
    You can do it within Postgres with pgcrypto. Or you can do it in your application using whatever encryption functions your programming language and framework provides. Commented Feb 24, 2021 at 19:21
  • @Schwern Can you give me the query, if possible? Commented Feb 25, 2021 at 4:18
  • 1
    See "Column-level encryption" in this blog post. enterprisedb.com/postgres-tutorials/… Commented Feb 25, 2021 at 7:21

1 Answer 1

2
CREATE EXTENSION pgcrypto;

SELECT 'column_name', crypt('column_name', gen_salt('md5')) AS hashed_value
FROM 'table_name';

Also, you can encrypt using 'sha1' instead of 'md5' as well. It is as below:

SELECT 'column_name', encode(digest("column_name",'sha1'),'hex') AS hashed_value
FROM 'table_name';
Sign up to request clarification or add additional context in comments.

1 Comment

have to say, crypt() is something like man 3 crypt: irreversibly “hash” phrase

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.