Timestamp in Postgresql table...

Posted on 2010-01-08
Last Modified: 2012-05-08
Is there a way to add some type of timestamp or date to a column in a table automatically via the DB itself verses Inserting using the timestamp from my application that is performing the DB operation?

Question by:Joel_Sisko
    LVL 12

    Accepted Solution

    Set the default value of the timestamp column to use the "now()" function.  Then when a record is added and no value is set for that column, PostgreSQL will use the value from 'now()'.  

    Of course, this method relies on NULL being set for the column in question when being inserted into the table.  If the application still sets a value for this, that value will be used instead of what would come from 'now()'.
    # new table
    CREATE TABLE your_table (
     created_on timestamp DEFAULT now(),
    # change an existing table
    ALTER TABLE your_old_table ALTER COLUMN created_on SET DEFAULT now();

    Open in new window

    LVL 12

    Author Comment

    Awesome thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
    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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now