Solved

How to create audit fields on a table in Postgresql

Posted on 2016-08-23
3
13 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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