Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:46:24+00:00 2026-05-29T05:46:24+00:00

How should I store my dates + times in a PostgreSQL database? This is

  • 0

How should I store my “dates + times” in a PostgreSQL database?

This is what I want to achieve:

  1. How can I have all the entries that occurred on (for example) 1 January 2012 00:00:00 local time anywhere in the world?
  2. Display all the entries sorted by date according to UTC time. (2012 New Year Eve in New York is more recent than the New Year in London).

How should I store my data? I have read that PostgreSQL stores all time in UTC internally (PostgreSQL documentation), so my users timezone is in fact lost.

I think I should use one column with type “timestamp without timezone”:

  • Point 1 is easy.
  • And with another column of type “String” I will store the timezone string (e.g : America/New_York)
    but then, point 2 seems still hard to do ….

I hope I am clear.

Edit new idea: I think with storing two timestamps: one without timezone (1. ok) and one with timezone (2. ok)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:46:25+00:00Added an answer on May 29, 2026 at 5:46 am

    Yes, PostgreSQL stores all timestamps as UTC internally. For a timestamp with time zone the time zone offset is only applied to adjust the time to UTC, but is not stored explicitly.

    I would not store the timezone string or even less a time zone abbreviation (those are not precise). This can later require expensive computation, because you have to consider daylight savings time and other oddities of the international time regime.

    You can either store the time zone offset as interval (takes 12 bytes) or a numerical amount of seconds (takes 4 bytes as integer) like I demonstrate in this related answer.

    Or, like you already proposed: store the local timestamp in addition to the UTC timestamp (takes 8 bytes). That would make your tasks easy. Consider the following demo::

    -- DROP TABLE tbl;
    CREATE TEMP TABLE tbl (id int, ts_tz timestamp with time zone, ts timestamp);
    INSERT INTO tbl VALUES
     (1,'2012-1-1 00:00+01','2012-1-1 00:00+01')
    ,(2,'2012-1-1 00:00+02','2012-1-1 00:00+02')
    ,(3,'2012-1-1 00:01+03','2012-1-1 00:01+03')
    ,(4,'2012-1-1 00:02+04','2012-1-1 00:02+04');
    

    Query for question 1:

    SELECT *
    FROM   tbl
    WHERE  ts = '2012-1-1 00:00'::timestamp;
    
     id |         ts_tz          |         ts
    ----+------------------------+---------------------
      1 | 2012-01-01 00:00:00+01 | 2012-01-01 00:00:00
      2 | 2011-12-31 23:00:00+01 | 2012-01-01 00:00:00
    

    Query for question 2:

    SELECT *
    FROM   tbl
    ORDER  BY ts_tz;
    
     id |         ts_tz          |         ts
    ----+------------------------+---------------------
      4 | 2011-12-31 21:02:00+01 | 2012-01-01 00:02:00
      3 | 2011-12-31 22:01:00+01 | 2012-01-01 00:01:00
      2 | 2011-12-31 23:00:00+01 | 2012-01-01 00:00:00
      1 | 2012-01-01 00:00:00+01 | 2012-01-01 00:00:00
    

    The tricky part with this solution may be to enter the local timestamp. That’s easy as long as all data is entered locally. But it needs consideration if you enter data for, say, New York in Los Angeles. Use the AT TIME ZONE construct for that:

    SELECT ('2012-1-1 00:00+00' AT TIME ZONE 'America/New_York')::timestamp
         , ('2012-1-1 00:00+00' AT TIME ZONE 'America/Los_Angeles')::timestamp
    
          timezone       |      timezone
    ---------------------+---------------------
     2011-12-31 19:00:00 | 2011-12-31 16:00:00
    

    Note how I use a timestamp with time zone as input. AT TIME ZONE gives different results for timestamps with or without time zone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database of entries with dates. So what I want is
We store all our dates SQL Server 2008 database in UTC time in DateTime
I have to choose the structure of a database that will store content types
MY previous site used DATETIME fileds in MySQL to store all dates/times. On my
I have a query that by all rights should not possibly fail, and I
I learnt that I should Store UTC and Show in local time. How can
We had a meeting this morning about how would should store our ID for
how should I store user data in asp.net mvc? Let's say a user want
Should I use decimal or float to store a ratio in a database? Particularly
I'm planning to make an application that needs to store dates way before 1970.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.