Solved

How to create audit fields on a table in Postgresql

Posted on 2016-08-23
3
10 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 143

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

837 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