<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Audit Transaction &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/audit-transaction/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 29 Mar 2017 12:55:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Create Audit Trigger, for logging or Auditing your Database DML Transactions</title>
		<link>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/</link>
					<comments>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 05 Apr 2017 16:16:38 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Audit DELETE]]></category>
		<category><![CDATA[Audit INSERT]]></category>
		<category><![CDATA[Audit Transaction]]></category>
		<category><![CDATA[Audit Trigger]]></category>
		<category><![CDATA[Audit UPDATE]]></category>
		<category><![CDATA[Data Security]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Logging DML]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3827</guid>

					<description><![CDATA[If you experienced with SQL Server, You might know about SQL Server Audit and Change Data Capture (CDC) functionality. As per the PostgreSQL WIKI, PostgreSQL does not have such an Inbuilt functionality for auditing your user transactions. Most of the Application requires, Database auditing like Who Inserted, When Updated kind of DML Transactions. Here, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&#038;title=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" data-a2a-url="https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/" data-a2a-title="PostgreSQL: Create Audit Trigger, for logging or Auditing your Database DML Transactions"></a></p><p>If you experienced with SQL Server, You might know about SQL Server Audit and Change Data Capture (CDC) functionality.<br />
As per the PostgreSQL WIKI, PostgreSQL does not have such an Inbuilt functionality for auditing your user transactions. </p>
<blockquote><p>Most of the Application requires, Database auditing like Who Inserted, When Updated kind of DML Transactions.<br />
Here, I am sharing one demonstration about Trigger Approach, which is not always good, but for specific requirement, we can go with this approach. </p></blockquote>
<p>I have created separate schema and table to log about DML transactions.<br />
PostgreSQL Database Administrator can use this trigger for security purpose also. </p>
<p><strong>Create a one new Schema:</strong></p>
<pre class="crayon-plain-tag">CREATE SCHEMA AuditTable;</pre> </p>
<p><strong>Create a new table for Logged Transaction:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE AuditTable.tbl_LoggedTransactions 
(
	SchemaName CHARACTER VARYING
	,TableName CHARACTER VARYING
	,UserName CHARACTER VARYING    
	,DMLAction CHARACTER VARYING
	,OriginalData TEXT
	,ExecutedNewData TEXT
	,ExecutedSQL TEXT
	,RecordDateTime TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()
);</pre> </p>
<p><strong>Revoke all access of newly creatd table from PUBLIC: </strong></p>
<pre class="crayon-plain-tag">REVOKE ALL ON AuditTable.tbl_LoggedTransactions FROM public;</pre> </p>
<p><strong>Create one trigger function for auditing DML transactions:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION AuditTable.trg_AuditDML() 
RETURNS TRIGGER 
AS $BODY$
DECLARE
    OldData TEXT;
    NewData TEXT;
BEGIN 
 
    IF (TG_OP = 'UPDATE') THEN
        OldData := ROW(OLD.*);
        NewData := ROW(NEW.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,OriginalData
		,ExecutedNewData
		,ExecutedSQL
	) 
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,OldData
		,NewData
		,current_query()
	);
        RETURN NEW;
    ELSIF (TG_OP = 'DELETE') THEN
        OldData := ROW(OLD.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,OriginalData
		,ExecutedSQL
	)
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,OldData
		,current_query()
	);
        RETURN OLD;
    ELSIF (TG_OP = 'INSERT') THEN
        NewData := ROW(NEW.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,ExecutedNewData
		,ExecutedSQL
	)
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,NewData
		,current_query()
	);
        RETURN NEW;
    ELSE
        RAISE WARNING '[AuditTable.trg_AuditDML] - Other action occurred: %, at %',TG_OP,now();
        RETURN NULL;
    END IF;
END;
$BODY$
LANGUAGE plpgsql;</pre> </p>
<p><strong>Create a sample table:<br />
</strong></p><pre class="crayon-plain-tag">CREATE TABLE public.tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING 
);</pre><p> </p>
<p><strong>Create trigger on tbl_Employees with the use of trigger function AuditTable.trg_AuditDML:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE TRIGGER trg_tbl_Employees_INSERT_UPDATE_DELETE
AFTER INSERT OR UPDATE OR DELETE ON public.tbl_Employees
FOR EACH ROW EXECUTE PROCEDURE AuditTable.trg_AuditDML();</pre> </p>
<p><strong>Execute few sample DMLs:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employees VALUES 
(1,'Anvesh'),(2,'Martin'),(3,'Roy')
,(4,'Jeeny'),(5,'Kavita'),(6,'Neevan');

UPDATE tbl_Employees SET EmpName='Mahi'
WHERE EmpID = 5;

DELETE FROM tbl_Employees WHERE EmpID = 4;</pre> </p>
<p><strong>Check AuditTable.tbl_LoggedTransactions table for logged DML transaction:<br />
</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM AuditTable.tbl_LoggedTransactions;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
