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?
-
1You 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.Schwern– Schwern2021-02-24 19:21:59 +00:00Commented Feb 24, 2021 at 19:21
-
@Schwern Can you give me the query, if possible?Mahesh G– Mahesh G2021-02-25 04:18:17 +00:00Commented Feb 25, 2021 at 4:18
-
1See "Column-level encryption" in this blog post. enterprisedb.com/postgres-tutorials/…Schwern– Schwern2021-02-25 07:21:46 +00:00Commented Feb 25, 2021 at 7:21
Add a comment
|
1 Answer
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';
1 Comment
yurenchen
have to say,
crypt() is something like man 3 crypt: irreversibly “hash” phrase