Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to create audit fields on a table in Postgresql

Posted on 2016-08-23
3
Medium Priority
?
32 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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