Solved

How to create audit fields on a table in Postgresql

Posted on 2016-08-23
3
7 Views
Last Modified: 2016-09-24
I need to create a Postgresql table that contains 4 'audit' fields:

  • User ID Created
  • User Id Modified
  • Date Created
  • Date Modified

These fields should be populated through the use of defaults and / or triggers.  For example, it is fine to use default values for all 4 fields when a record is created, but after creation, the "user id created" and "date created" fields should never change.  On the other hand, I want the "user id modified" and "date modified" to change every time that the record gets updated.

I am looking for someone to help me complete the following "create table" statement:

create table test_table (id serial
                       , name character varying
                       , user_id_created
                       , user_id_modified 
                       , datetime_created 
                       , datetime_modified
                       )

Open in new window

0
Comment
Question by:jbaird123
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points (awarded by participants)
ID: 41766813
to deny updates on the created fields, you could deny them, but that is not part of the
DENY UPDATE ON test_table ( user_id_created) TO <users or group as you want>

to fill them on creation:
user name is a system function: current_user or user:
https://www.postgresql.org/docs/9.1/static/functions-info.html
and date/time functions:
https://www.postgresql.org/docs/9.1/static/functions-datetime.html

to specify the "default" in the syntax:
https://www.postgresql.org/docs/9.2/static/ddl-default.html

to update the "update" fields, use a trigger on the table:
https://www.postgresql.org/docs/9.4/static/sql-createtrigger.html
0
 

Author Comment

by:jbaird123
ID: 41766819
Thank you!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now