{"id":4221,"date":"2018-10-26T06:52:33","date_gmt":"2018-10-26T06:52:33","guid":{"rendered":"https:\/\/dbtut.com\/?p=4221"},"modified":"2018-11-27T16:21:52","modified_gmt":"2018-11-27T16:21:52","slug":"column-encryption-with-postgresql-trigger","status":"publish","type":"post","link":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/","title":{"rendered":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In Postgresql, we can capture the record with the trigger, encrypt and write to the table.<\/p>\n<p>Let&#8217;s create the table,<\/p>\n<pre class=\"lang:default decode:true \">CREATE TABLE example_table ( syndicate text,deed text,hash bytea default \u2018AES_KEY\u2019);<\/pre>\n<p>&nbsp;<\/p>\n<p>Then we create our function,<\/p>\n<pre class=\"lang:default decode:true\">CREATE OR REPLACE FUNCTION hash_update_tg() RETURNS trigger AS $$\r\nBEGIN\r\nIF tg_op = \u2018INSERT\u2019 OR tg_op = \u2018UPDATE\u2019 THEN\r\nNEW.syndicate = PGP_SYM_ENCRYPT(NEW.syndicate,\u2019AES_KEY\u2019); \u2013digest(NEW.key_codes, \u2018sha256\u2019);\r\nNEW.deed = PGP_SYM_ENCRYPT(NEW.deed,\u2019AES_KEY\u2019);\u2013digest(NEW.key_codes, \u2018sha256\u2019);\r\nRETURN NEW;\r\nEND IF;\r\nEND;\r\n$$ LANGUAGE plpgsql;<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, we create our trigger.<\/p>\n<pre class=\"lang:default decode:true \">CREATE TRIGGER example_table_hash_insert_update\r\nBEFORE INSERT OR UPDATE ON example_table\r\nFOR EACH ROW EXECUTE PROCEDURE hash_update_tg();<\/pre>\n<p>&nbsp;<\/p>\n<p>Then, when we add records to the table, we can see that the data is encrypted.<\/p>\n<pre class=\"lang:default decode:true\">insert into example_table values ('syndicate_name','x');\r\nselect * from example_table;<\/pre>\n<p>&nbsp;<\/p>\n<p>With the following sql we can see the unencrypted version.<\/p>\n<pre class=\"lang:default decode:true \">select pgp_sym_decrypt(syndicate::bytea, \u2018AES_KEY\u2019),pgp_sym_decrypt(deed::bytea, \u2018AES_KEY\u2019) from example_table;<\/pre>\n<p>I hope it has been helpful.<\/p>\n\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4221\" class=\"pvc_stats all  \" data-element-id=\"4221\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In Postgresql, we can capture the record with the trigger, encrypt and write to the table. Let&#8217;s create the table, CREATE TABLE example_table ( syndicate text,deed text,hash bytea default \u2018AES_KEY\u2019); &nbsp; Then we create our function, CREATE OR REPLACE FUNCTION hash_update_tg() RETURNS trigger AS $$ BEGIN IF tg_op = \u2018INSERT\u2019 OR tg_op = \u2018UPDATE\u2019 &hellip;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_4221\" class=\"pvc_stats all  \" data-element-id=\"4221\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":316,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4221","post","type-post","status-publish","format-standard","","category-postgres"],"aioseo_notices":[],"a3_pvc":{"activated":true,"total_views":1627,"today_views":0},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials<\/title>\n<meta name=\"description\" content=\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials\" \/>\n<meta property=\"og:description\" content=\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\" \/>\n<meta property=\"og:site_name\" content=\"Database Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-26T06:52:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-11-27T16:21:52+00:00\" \/>\n<meta name=\"author\" content=\"Engin Yilmaz\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Engin Yilmaz\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\"},\"author\":{\"name\":\"Engin Yilmaz\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/b1a386d05873bd038726ab40496e458a\"},\"headline\":\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER\",\"datePublished\":\"2018-10-26T06:52:33+00:00\",\"dateModified\":\"2018-11-27T16:21:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\"},\"wordCount\":75,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\",\"url\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\",\"name\":\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/#website\"},\"datePublished\":\"2018-10-26T06:52:33+00:00\",\"dateModified\":\"2018-11-27T16:21:52+00:00\",\"description\":\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER\",\"breadcrumb\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dbtut.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dbtut.com\/#website\",\"url\":\"https:\/\/dbtut.com\/\",\"name\":\"Database Tutorials\",\"description\":\"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux\",\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dbtut.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dbtut.com\/#organization\",\"name\":\"dbtut\",\"url\":\"https:\/\/dbtut.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"width\":223,\"height\":36,\"caption\":\"dbtut\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/b1a386d05873bd038726ab40496e458a\",\"name\":\"Engin Yilmaz\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1beca465a583c4518bafb41d560f2c385f15b4c6810b4c396204e58fb07520ea?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1beca465a583c4518bafb41d560f2c385f15b4c6810b4c396204e58fb07520ea?s=96&d=mm&r=g\",\"caption\":\"Engin Yilmaz\"},\"url\":\"https:\/\/dbtut.com\/index.php\/author\/enginyilmaz\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials","description":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/","og_locale":"en_US","og_type":"article","og_title":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials","og_description":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER","og_url":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/","og_site_name":"Database Tutorials","article_published_time":"2018-10-26T06:52:33+00:00","article_modified_time":"2018-11-27T16:21:52+00:00","author":"Engin Yilmaz","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Engin Yilmaz","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#article","isPartOf":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/"},"author":{"name":"Engin Yilmaz","@id":"https:\/\/dbtut.com\/#\/schema\/person\/b1a386d05873bd038726ab40496e458a"},"headline":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER","datePublished":"2018-10-26T06:52:33+00:00","dateModified":"2018-11-27T16:21:52+00:00","mainEntityOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/"},"wordCount":75,"commentCount":0,"publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"articleSection":["PostgreSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/","url":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/","name":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER - Database Tutorials","isPartOf":{"@id":"https:\/\/dbtut.com\/#website"},"datePublished":"2018-10-26T06:52:33+00:00","dateModified":"2018-11-27T16:21:52+00:00","description":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER","breadcrumb":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dbtut.com\/index.php\/2018\/10\/26\/column-encryption-with-postgresql-trigger\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dbtut.com\/"},{"@type":"ListItem","position":2,"name":"COLUMN ENCRYPTION WITH POSTGRESQL TRIGGER"}]},{"@type":"WebSite","@id":"https:\/\/dbtut.com\/#website","url":"https:\/\/dbtut.com\/","name":"Database Tutorials","description":"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux","publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dbtut.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/dbtut.com\/#organization","name":"dbtut","url":"https:\/\/dbtut.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","width":223,"height":36,"caption":"dbtut"},"image":{"@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/dbtut.com\/#\/schema\/person\/b1a386d05873bd038726ab40496e458a","name":"Engin Yilmaz","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1beca465a583c4518bafb41d560f2c385f15b4c6810b4c396204e58fb07520ea?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1beca465a583c4518bafb41d560f2c385f15b4c6810b4c396204e58fb07520ea?s=96&d=mm&r=g","caption":"Engin Yilmaz"},"url":"https:\/\/dbtut.com\/index.php\/author\/enginyilmaz\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/4221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/users\/316"}],"replies":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/comments?post=4221"}],"version-history":[{"count":0,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/4221\/revisions"}],"wp:attachment":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/media?parent=4221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/categories?post=4221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/tags?post=4221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}