{"id":1752,"date":"2025-01-15T22:12:02","date_gmt":"2025-01-15T15:12:02","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1752"},"modified":"2025-01-16T08:05:54","modified_gmt":"2025-01-16T01:05:54","slug":"postgresql-before-insert-trigger","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/","title":{"rendered":"PostgreSQL BEFORE INSERT Trigger"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>BEFORE INSERT<\/code> trigger to automatically execute a function before inserting data into a table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='an-overview-of-postgresql-before-insert-trigger'>An Overview of PostgreSQL BEFORE INSERT Trigger <a href=\"#an-overview-of-postgresql-before-insert-trigger\" class=\"anchor\" id=\"an-overview-of-postgresql-before-insert-trigger\" title=\"Anchor for An Overview of PostgreSQL BEFORE INSERT Trigger\">#<\/a><\/h2>\n\n\n\n<p>A <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\" target=\"_blank\" rel=\"noreferrer noopener\">trigger<\/a> is a database object associated with a table and is fired automatically when an event occurs on the table.<\/p>\n\n\n\n<p>A <code>BEFORE INSERT<\/code> trigger is fired before an <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\" target=\"_blank\" rel=\"noreferrer noopener\">INSERT<\/a> statement is executed. When the trigger fires, it automatically calls a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-function\/\">user-defined function<\/a> to perform a specific task.<\/p>\n\n\n\n<p>For example, the trigger function can validate data across tables to ensure data integrity before <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">inserting data into a table<\/a>.<\/p>\n\n\n\n<p>In the trigger function, you can access the row that will be inserted via the <code>NEW<\/code> <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-record\/\">record variable<\/a>. To retrieve a specific column value from this row, you use dot notation like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-built_in\">NEW<\/span>.<span class=\"hljs-built_in\">column_name<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To create a <code>BEFORE INSERT<\/code> trigger, you use the following steps:<\/p>\n\n\n\n<p><strong>Step 1.<\/strong> Create a trigger function that will be invoked before the insert event occurs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> trigger_function () \n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">TRIGGER<\/span> \n<span class=\"hljs-keyword\">AS<\/span> \n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">BEGIN<\/span>\n\u00a0 \u00a0\u00a0<span class=\"hljs-comment\">-- function body<\/span>\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-built_in\">NEW<\/span>;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> \n<span class=\"hljs-keyword\">LANGUAGE<\/span> PLPGSQL;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 2. <\/strong>Create a new trigger using the <code>CREATE TRIGGER<\/code> statement and associate the trigger function with the trigger:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> trigger_name\n<span class=\"hljs-keyword\">BEFORE<\/span> <span class=\"hljs-keyword\">INSERT<\/span>\n<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">EACH<\/span> { <span class=\"hljs-keyword\">ROW<\/span> | <span class=\"hljs-keyword\">STATEMENT<\/span> }\n<span class=\"hljs-keyword\">EXECUTE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> trigger_function();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the trigger name after the <code>CREATE TRIGGER<\/code> keyword.<\/li>\n\n\n\n<li>Next, use the <code>BEFORE INSERT<\/code> trigger timing to indicate that the trigger should fire before an insert event.<\/li>\n\n\n\n<li>Then, specify the table name to which the trigger belongs in the <code>ON<\/code> clause.<\/li>\n\n\n\n<li>After that, define the row-level or statement-level trigger using the <code>FOR EACH ROW<\/code> or <code>STATEMENT<\/code>.<\/li>\n\n\n\n<li>Finally, provide the name of the function you want to execute when the trigger fires in the <code>EXECUTE FUNCTION<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-before-insert-trigger-example'>PostgreSQL BEFORE INSERT Trigger Example <a href=\"#postgresql-before-insert-trigger-example\" class=\"anchor\" id=\"postgresql-before-insert-trigger-example\" title=\"Anchor for PostgreSQL BEFORE INSERT Trigger Example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a trigger that validates an inventory transaction in the <code>transactions<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"208\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png\" alt=\"\" class=\"wp-image-1054\"\/><\/figure>\n\n\n\n<p>that does not result in a negative quantity in the <code>inventories<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"139\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png\" alt=\"\" class=\"wp-image-1046\"\/><\/figure>\n\n\n\n<p>First, create a trigger function that validates the inventory based on the transaction:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> validate_inventory()\n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">TRIGGER<\/span> \n<span class=\"hljs-keyword\">AS<\/span> $$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n    v_quantity <span class=\"hljs-type\">INT<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-comment\">-- Get the current inventory level<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> quantity <span class=\"hljs-keyword\">INTO<\/span> v_quantity\n    <span class=\"hljs-keyword\">FROM<\/span> inventories\n    <span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-built_in\">NEW<\/span>.product_id \n      <span class=\"hljs-keyword\">AND<\/span> warehouse_id = <span class=\"hljs-built_in\">NEW<\/span>.warehouse_id;\n\n    <span class=\"hljs-comment\">-- If the transaction is an issue, ensure it does not lead <\/span>\n    <span class=\"hljs-comment\">-- to negative inventory<\/span>\n    <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-built_in\">NEW<\/span>.<span class=\"hljs-keyword\">type<\/span> = <span class=\"hljs-string\">'issue'<\/span> <span class=\"hljs-keyword\">THEN<\/span>\n        <span class=\"hljs-keyword\">IF<\/span> v_quantity <span class=\"hljs-keyword\">IS<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">OR<\/span> v_quantity &lt; <span class=\"hljs-built_in\">NEW<\/span>.quantity <span class=\"hljs-keyword\">THEN<\/span>\n            <span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">EXCEPTION<\/span> <span class=\"hljs-string\">'Insufficient inventory for product_id %, warehouse_id %'<\/span>, \n                            <span class=\"hljs-built_in\">NEW<\/span>.product_id, <span class=\"hljs-built_in\">NEW<\/span>.warehouse_id;\n        <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\n    <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-built_in\">NEW<\/span>;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> \n<span class=\"hljs-keyword\">LANGUAGE<\/span> plpgsql;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIE9SIFJFUExBQ0UgRlVOQ1RJT04gdmFsaWRhdGVfaW52ZW50b3J5KCkKUkVUVVJOUyBUUklHR0VSIApBUyAkJApERUNMQVJFCiAgICB2X3F1YW50aXR5IElOVDsKQkVHSU4KICAgIC0tIEZldGNoIHRoZSBjdXJyZW50IGludmVudG9yeSBsZXZlbCBmb3IgdGhlIHByb2R1Y3QgaW4gdGhlIHNwZWNpZmllZCB3YXJlaG91c2UKICAgIFNFTEVDVCBxdWFudGl0eSBJTlRPIHZfcXVhbnRpdHkKICAgIEZST00gaW52ZW50b3JpZXMKICAgIFdIRVJFIHByb2R1Y3RfaWQgPSBORVcucHJvZHVjdF9pZCAKICAgICAgQU5EIHdhcmVob3VzZV9pZCA9IE5FVy53YXJlaG91c2VfaWQ7CgogICAgLS0gSWYgdGhlIHRyYW5zYWN0aW9uIGlzIGFuIGlzc3VlLCBlbnN1cmUgaXQgZG9lcyBub3QgbGVhZCB0byBuZWdhdGl2ZSBpbnZlbnRvcnkKICAgIElGIE5FVy50eXBlID0gJ2lzc3VlJyBUSEVOCiAgICAgICAgSUYgdl9xdWFudGl0eSBJUyBOVUxMIE9SIHZfcXVhbnRpdHkgPCBORVcucXVhbnRpdHkgVEhFTgogICAgICAgICAgICBSQUlTRSBFWENFUFRJT04gJ0luc3VmZmljaWVudCBpbnZlbnRvcnkgZm9yIHByb2R1Y3RfaWQgJSwgd2FyZWhvdXNlX2lkICUnLCAKICAgICAgICAgICAgICAgICAgICAgICAgICAgIE5FVy5wcm9kdWN0X2lkLCBORVcud2FyZWhvdXNlX2lkOwogICAgICAgIEVORCBJRjsKICAgIEVORCBJRjsKCiAgICBSRVRVUk4gTkVXOwpFTkQ7CiQkIExBTkdVQUdFIHBscGdzcWw7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, create a trigger on the <code>transactions<\/code> table, which associates with the <code>validate_inventory<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> before_insert_transaction\n<span class=\"hljs-keyword\">BEFORE<\/span> <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">ON<\/span> transactions\n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">EACH<\/span> <span class=\"hljs-keyword\">ROW<\/span>\n<span class=\"hljs-keyword\">EXECUTE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> validate_inventory();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIFRSSUdHRVIgYmVmb3JlX2luc2VydF90cmFuc2FjdGlvbgpCRUZPUkUgSU5TRVJUIE9OIHRyYW5zYWN0aW9ucwpGT1IgRUFDSCBST1cKRVhFQ1VURSBGVU5DVElPTiB2YWxpZGF0ZV9pbnZlbnRvcnkoKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, get the inventory quantity of the product id 2 at the warehouse id 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> * \n<span class=\"hljs-keyword\">FROM<\/span> inventories\n<span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">AND<\/span> \n      warehouse_id = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUICogRlJPTSBpbnZlbnRvcmllcyBXSEVSRSBwcm9kdWN0X2lkID0gMiBBTkQgd2FyZWhvdXNlX2lkID0gMjs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> inventory_id | product_id | warehouse_id | quantity\n<span class=\"hljs-comment\">--------------+------------+--------------+----------<\/span>\n            <span class=\"hljs-number\">2<\/span> |          <span class=\"hljs-number\">2<\/span> |            <span class=\"hljs-number\">2<\/span> |      <span class=\"hljs-number\">150<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, attempt to create a transaction that issues 200 pieces of product id 2 at warehouse id 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> transactions (\n    product_id,\n    warehouse_id,\n    user_id,\n    <span class=\"hljs-keyword\">type<\/span>,\n    quantity,\n    transaction_date\n  )\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'issue'<\/span>, <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-string\">'2025-01-15'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=SU5TRVJUIElOVE8gdHJhbnNhY3Rpb25zIChwcm9kdWN0X2lkLCB3YXJlaG91c2VfaWQsIHVzZXJfaWQsIHR5cGUsIHF1YW50aXR5LCB0cmFuc2FjdGlvbl9kYXRlKSBWQUxVRVMgKDIsIDIsIDEsICdpc3N1ZScsIDIwMCwgJzIwMjUtMDEtMTUnKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>PostgreSQL issues the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">ERROR: Insufficient inventory <span class=\"hljs-keyword\">for<\/span> product_id <span class=\"hljs-number\">2<\/span>, warehouse_id <span class=\"hljs-number\">2<\/span>\nCONTEXT: PL\/pgSQL <span class=\"hljs-keyword\">function<\/span> validate_inventory() <span class=\"hljs-type\">line<\/span> <span class=\"hljs-number\">13<\/span> at <span class=\"hljs-keyword\">RAISE<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, create a valid transaction that issues a valid quantity:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> transactions (product_id, warehouse_id, user_id, <span class=\"hljs-keyword\">type<\/span>, quantity, transaction_date)\n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'issue'<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'2025-01-15'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=SU5TRVJUIElOVE8gdHJhbnNhY3Rpb25zIChwcm9kdWN0X2lkLCB3YXJlaG91c2VfaWQsIHVzZXJfaWQsIHR5cGUsIHF1YW50aXR5LCB0cmFuc2FjdGlvbl9kYXRlKSBWQUxVRVMgKDIsIDIsIDEsICdpc3N1ZScsIDIwLCAnMjAyNS0wMS0xNScpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p class=\"note\">Note that you can create a <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-after-insert-trigger\/\">AFTER INSERT trigger<\/a> to update the inventory quantity in the <code>inventories<\/code> table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>BEFORE INSERT<\/code> triggers to execute a function automatically before inserting a new row into a table.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=before-insert-trigger\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1752\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL BEFORE INSERT Trigger\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1752\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL BEFORE INSERT Trigger\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function before inserting data into a table. An Overview of PostgreSQL BEFORE INSERT Trigger # A trigger is a database object associated with a table and is fired automatically when an event occurs on the table. A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":101,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1752","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL BEFORE INSERT Trigger<\/title>\n<meta name=\"description\" content=\"You&#039;ll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL BEFORE INSERT Trigger\" \/>\n<meta property=\"og:description\" content=\"You&#039;ll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-16T01:05:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png\" \/>\n\t<meta property=\"og:image:width\" content=\"144\" \/>\n\t<meta property=\"og:image:height\" content=\"208\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/\",\"name\":\"PostgreSQL BEFORE INSERT Trigger\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/transactions.png\",\"datePublished\":\"2025-01-15T15:12:02+00:00\",\"dateModified\":\"2025-01-16T01:05:54+00:00\",\"description\":\"You'll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/transactions.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/transactions.png\",\"width\":144,\"height\":208,\"caption\":\"transactions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-before-insert-trigger\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL BEFORE INSERT Trigger\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL BEFORE INSERT Trigger","description":"You'll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.","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:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL BEFORE INSERT Trigger","og_description":"You'll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-16T01:05:54+00:00","og_image":[{"width":144,"height":208,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/","name":"PostgreSQL BEFORE INSERT Trigger","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png","datePublished":"2025-01-15T15:12:02+00:00","dateModified":"2025-01-16T01:05:54+00:00","description":"You'll learn how to use the PostgreSQL BEFORE INSERT trigger to automatically execute a function after an before inserting data into a table.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/transactions.png","width":144,"height":208,"caption":"transactions"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-before-insert-trigger\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL BEFORE INSERT Trigger"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1752"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1752\/revisions"}],"predecessor-version":[{"id":1767,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1752\/revisions\/1767"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}