Database Administrators
Q&A for database professionals who wish to improve their database skills
Latest Questions
39
votes
2
answers
70646
views
How to best store a timestamp in PostgreSQL?
I'm working on a PostgreSQL DB design and I am wondering how best to store timestamps. ### Assumptions Users in different timezones will use the database for all CRUD functions. I have looked at 2 options: - `timestamp NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')` - `bigint NOT NULL DEFAULT` For **`t...
I'm working on a PostgreSQL DB design and I am wondering how best to store timestamps.
### Assumptions
Users in different timezones will use the database for all CRUD functions.
I have looked at 2 options:
-
-
timestamp NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')
- bigint NOT NULL DEFAULT
For **timestamp
** I would send a string that would represent the exact (UTC) timestamp for the INSERT moment.
For **bigint
** I would store the exact same thing, but in a number format. (time zone issues are handled before millis is handed over to the server, so always millis in UTC.)
One main advantage with storing a bigint
could be that it would be easier to store and to retrieve, as passing a correctly formatted timestamp is more complex than a simple number (millis since Unix Epoc).
My question is which one would allow for the most flexible design and what could be the pitfalls of each approach.
Bam
(579 rep)
Jul 19, 2015, 08:08 PM
• Last activity: Nov 21, 2024, 10:34 PM
0
votes
0
answers
126
views
Relationship between /etc/localtime and Etc/UTC in PostgreSQL
I run our company's DBMS in containers, and when creating a container, the ansible deployment role mounts the host file `/etc/localtime` along the same path into the container (although, as far as I understand, the behavior would be the same if the DBMS was running as a service on the host). In prev...
I run our company's DBMS in containers, and when creating a container, the ansible deployment role mounts the host file
/etc/localtime
along the same path into the container (although, as far as I understand, the behavior would be the same if the DBMS was running as a service on the host).
In previous instances, the local time zone (other than UTC) was set in postgresql.conf
, but now one of the development teams has decided to work with the UTC time zone in DBMS. I created a new test instance (version 15.3
) for them and specified TimeZone = 'Etc/UTC'
in the configuration, and an non-obvious problem arose, which cost me a considerable number of working hours before I finally figured out what was happening:
Despite the fact that the query show timezone;
showed the time zone Etc/UTC
, in fact all requests related to the time output (for example select current_timestamp;
) worked as if the time zone from /etc/localtime
was really there. If I'm through SET TimeZone TO '…';
specified other time zone except Etc/UTC
— the behavior became expected, this other time zone was applied. When returning to Etc/UTC
, the time zone from /etc/localtime
was returned again.
Etc/UTC
behaves as expected only if I did not mount the file from the host at all, in which case a file with the UTC zone from the container image is used. However, I think in this case most likely the behavior is not really different, it's just that here the time zone from the file coincided with the DBMS setting.
Does the Etc/UTC
time zone have any special meaning (something like «consider that the system time zone is used as UTC») or is this not a feature but a bug?
What would be more correct for me to do: remove the /etc/localtime
mount and use Etc/UTC
or mount the file and change the time zone to, for example, GMT
or +00
?
## Addition
Queries (executed in pgcli
):
> show timezone;
+----------+
| TimeZone |
|----------|
| Etc/UTC |
+----------+
> select * from pg_timezone_names limit 2000;
+----------------------------------------+--------+------------------+--------+
| name | abbrev | utc_offset | is_dst |
|----------------------------------------+--------+------------------+--------|
| … | | | |
| Etc/GMT0 | GMT | 0:00:00 | False |
| Etc/Greenwich | GMT | 0:00:00 | False |
| Etc/UCT | CEST | 2:00:00 | True |
| Etc/UTC | CEST | 2:00:00 | True |
| Etc/Universal | CEST | 2:00:00 | True |
| Etc/Zulu | CEST | 2:00:00 | True |
| … | | | |
+----------------------------------------+--------+------------------+--------+
> select current_timestamp;
+-------------------------------+
| current_timestamp |
|-------------------------------|
| 2024-04-04 06:18:05.994608+02 |
+-------------------------------+
> set TimeZone to 'Etc/GMT';
> select current_timestamp;
+-------------------------------+
| current_timestamp |
|-------------------------------|
| 2024-04-04 04:21:06.581204+00 |
+-------------------------------+
strafer
(1 rep)
Apr 2, 2024, 05:16 PM
• Last activity: Apr 4, 2024, 04:28 AM
50
votes
6
answers
173072
views
MySQL Set UTC time as default timestamp
How do I set a timestamp column whose default value is the current UTC time? MySQL uses `UTC_TIMESTAMP()` function for UTC timestamp: mysql> SELECT UTC_TIMESTAMP(); +---------------------+ | UTC_TIMESTAMP() | +---------------------+ | 2012-07-01 11:36:35 | +---------------------+ 1 row in set (0.00...
How do I set a timestamp column whose default value is the current UTC time?
MySQL uses
UTC_TIMESTAMP()
function for UTC timestamp:
mysql> SELECT UTC_TIMESTAMP();
+---------------------+
| UTC_TIMESTAMP() |
+---------------------+
| 2012-07-01 11:36:35 |
+---------------------+
1 row in set (0.00 sec)
So I've tried:
CREATE TABLE blah
(
creation_time
TIMESTAMP DEFAULT UTC_TIMESTAMP,
...
And other variations, like UTC_TIMESTAMP()
, but without success.
Adam Matan
(12079 rep)
Jul 1, 2012, 11:33 AM
• Last activity: Feb 20, 2023, 01:48 PM
11
votes
1
answers
6591
views
Oddities with AT TIME ZONE and UTC offsets
I don't understand the difference between these two columns. America/Chicago timezone is UTC-6, so I expect both to return the same result: select timezone('America/Chicago', '2017-01-01 12:00:00'::TIMESTAMP AT TIME ZONE 'UTC'), timezone('UTC-6' , '2017-01-01 12:00:00'::TIMESTAMP AT TIME ZONE 'UTC')...
I don't understand the difference between these two columns. America/Chicago timezone is UTC-6, so I expect both to return the same result:
select timezone('America/Chicago', '2017-01-01 12:00:00'::TIMESTAMP AT TIME ZONE 'UTC'),
timezone('UTC-6' , '2017-01-01 12:00:00'::TIMESTAMP AT TIME ZONE 'UTC');
However, the result is:
2017-01-01 06:00:00 | 2017-01-01 18:00:00
Also, this behavior is highly awkward,
SELECT '1:00 -1'::time with time zone AT TIME ZONE '-1';
timezone
-------------
03:00:00+01
Can anyone explain this?
Asaf David
(245 rep)
Jan 5, 2017, 02:11 PM
• Last activity: Mar 21, 2022, 04:51 AM
1
votes
1
answers
210
views
Why is Oracle's TZ_OFFSET wrong for all cities on only one server?
Why does Oracle think that the timezone offset of Denver is UTC-7:00? The server time and systimestamp look right. **Server 1: Oracle 11.2.0.4.0** SELECT SESSIONTIMEZONE FROM DUAL; America/Denver cat /etc/sysconfig/clock ZONE="America/Denver" SELECT * FROM v$timezone_file; timezlrg_14.dat 14 SELECT...
Why does Oracle think that the timezone offset of Denver is UTC-7:00?
The server time and systimestamp look right.
**Server 1: Oracle 11.2.0.4.0**
SELECT SESSIONTIMEZONE FROM DUAL;
America/Denver
cat /etc/sysconfig/clock
ZONE="America/Denver"
SELECT * FROM v$timezone_file;
timezlrg_14.dat 14
SELECT systimestamp FROM dual;
29-JUN-21 01.29.21.546036000 PM -06:00
SELECT tzabbrev, SUBSTR(tz_offset(tzname), 1, 6), tzname FROM v$timezone_names tz WHERE tzname = 'America/Denver';
LMT -06:00 America/Denver
MST -06:00 America/Denver
MDT -06:00 America/Denver
MWT -06:00 America/Denver
MPT -06:00 America/Denver
**Server 2: Oracle 11.1.0.6.0**
SELECT SESSIONTIMEZONE FROM DUAL;
America/Denver
cat /etc/sysconfig/clock
ZONE="America/Denver"
UTC=false
ARC=false
SELECT * FROM v$timezone_file;
timezlrg.dat 4
SELECT tzabbrev, SUBSTR(tz_offset(tzname), 1, 6), tzname FROM v$timezone_names tz WHERE tzname = 'America/Denver';
LMT -07:00 America/Denver
MST -07:00 America/Denver
MWT -07:00 America/Denver
MDT -07:00 America/Denver
Charles Burns
(251 rep)
Jun 29, 2021, 07:52 PM
• Last activity: Jul 2, 2021, 06:51 AM
35
votes
11
answers
232340
views
How can I get the correct offset between UTC and local times for a date that is before or after DST?
I currently use the following to get a local datetime from a UTC datetime: SET @offset = DateDiff(minute, GetUTCDate(), GetDate()) SET @localDateTime = DateAdd(minute, @offset, @utcDateTime) My problem is that if daylight savings time occurs between `GetUTCDate()` and `@utcDateTime`, the `@localDate...
I currently use the following to get a local datetime from a UTC datetime:
SET @offset = DateDiff(minute, GetUTCDate(), GetDate())
SET @localDateTime = DateAdd(minute, @offset, @utcDateTime)
My problem is that if daylight savings time occurs between
GetUTCDate()
and @utcDateTime
, the @localDateTime
ends up being an hour off.
Is there an easy way to convert from utc to local time for a date that is not the current date?
I'm using SQL Server 2005
Rachel
(8557 rep)
Nov 5, 2012, 01:25 PM
• Last activity: Dec 9, 2020, 09:58 PM
2
votes
1
answers
205
views
Compute Leap Seconds
I use PostgreSQL 11 and have two columns. One is unix timestamp which is not aware of leap seconds and an utc timestamp which is aware of leap seconds. Is there a way either to find the number of leap seconds between an unix timestamp (without leap Seconds) and an utc timestamp (with leap seconds)?...
I use PostgreSQL 11 and have two columns. One is unix timestamp which is not aware of leap seconds and an utc timestamp which is aware of leap seconds.
Is there a way either to find the number of leap seconds between an unix timestamp (without leap Seconds) and an utc timestamp (with leap seconds)?
Alternatively is there a way to convert an utc timestamp (with leap seconds) into an unix timestamp (without leap seconds)?
nali
(206 rep)
Feb 21, 2019, 10:10 AM
• Last activity: Apr 21, 2020, 12:05 PM
0
votes
2
answers
527
views
UTC time showing wrong
UTC time is showing incorrectly in mysql 5.1. currently, it is 9 AM mysql> SELECT UTC_TIMESTAMP, UTC_TIMESTAMP( ); +---------------------+---------------------+ | UTC_TIMESTAMP | UTC_TIMESTAMP( ) | +---------------------+---------------------+ | 2018-08-01 14:15:03 | 2018-08-01 14:15:03 | +---------...
UTC time is showing incorrectly in mysql 5.1. currently, it is 9 AM
mysql> SELECT UTC_TIMESTAMP, UTC_TIMESTAMP( );
+---------------------+---------------------+
| UTC_TIMESTAMP | UTC_TIMESTAMP( ) |
+---------------------+---------------------+
| 2018-08-01 14:15:03 | 2018-08-01 14:15:03 |
+---------------------+---------------------+
1 row in set (0.00 sec)
SELECT current_timestamp dt1, utc_timestamp dt2, @@global.time_zone tz1, @@session.time_zone tz2;
+---------------------+---------------------+--------+--------+
| dt1 | dt2 | tz1 | tz2 |
+---------------------+---------------------+--------+--------+
| 2018-08-01 09:15:56 | 2018-08-01 14:15:56 | SYSTEM | SYSTEM |
+---------------------+---------------------+--------+--------+
1 row in set (0.01 sec)
Please let me know how to fix it.
Siva
(101 rep)
Aug 1, 2018, 07:58 AM
• Last activity: Aug 1, 2018, 11:00 AM
0
votes
1
answers
1983
views
convert historic local time to UTC taking into account of daylight saving
How should I convert historic local time to UTC taking into account of daylight saving? The main problem is the daylight saving doesn't occur at a fixed period every year and hence I cannot think of a way to do this task.
How should I convert historic local time to UTC taking into account of daylight saving?
The main problem is the daylight saving doesn't occur at a fixed period every year and hence I cannot think of a way to do this task.
Prashant Bhanarkar
(143 rep)
Jun 22, 2016, 08:04 AM
• Last activity: Jun 22, 2016, 09:11 AM
0
votes
1
answers
1068
views
Query MST instead of UTC for timestamps
on SQL Server, i'm trying to query all of last months data based off of the time stamp, however, all those timestamps are in UTC and i need them to be in MST (the reason is mainly for the end dates) So far this is what I have: Select Vehicle.Timestamp WHERE Vehicle.Timestamp >= DATEADD(mm,DATEDIFF(m...
on SQL Server, i'm trying to query all of last months data based off of the time stamp, however, all those timestamps are in UTC and i need them to be in MST (the reason is mainly for the end dates)
So far this is what I have:
Select Vehicle.Timestamp
WHERE Vehicle.Timestamp >= DATEADD(mm,DATEDIFF(mm,32,GETUTCDATE()),0)
AND Vehicle.Timestamp < DATEADD(mm,DATEDIFF(mm,0,GETUTCDATE()),0)
order by Vehicle.Timestamp
i'm not entirely sure which way to move forward.
I've seen the use of
switchoffset
however, not sure the best way to apply it.
scaborski
(3 rep)
Feb 2, 2015, 07:55 PM
• Last activity: Feb 2, 2015, 10:40 PM
1
votes
2
answers
2211
views
Get time format in PostgreSQL
If I want get Ubuntu system time, I have two options: $ cat /etc/timezone US/Eastern $ date Sun Sep 15 14:45:02 EDT 2013 How can I find out if PostgreSQL is using utc or a utc offset, such as US/Eastern?
If I want get Ubuntu system time, I have two options:
$ cat /etc/timezone
US/Eastern
$ date
Sun Sep 15 14:45:02 EDT 2013
How can I find out if PostgreSQL is using utc or a utc offset, such as US/Eastern?
JohnMerlino
(1939 rep)
Sep 15, 2013, 06:47 PM
• Last activity: Sep 16, 2013, 02:18 PM
2
votes
1
answers
174
views
WorldWide site - should I save UTC or in fixed time?
Let's say I have a world wide website ( I mean it has uses from all around the world). I have 1 server which is in germany ( at some city). it will never be moved from germany - ever. My question is : When users saves data , I need to save the action date : Should I save ( and I think I do) it as `U...
Let's say I have a world wide website ( I mean it has uses from all around the world).
I have 1 server which is in germany ( at some city). it will never be moved from germany - ever.
My question is :
When users saves data , I need to save the action date :
Should I save ( and I think I do) it as
UTC
?
Or since - the server is in one location - it is ok to save all the dates as GetDATE()
so the point of relativity is the same point.
Am I wrong ? If I do , can you please supply a simple scenario where it's gonna fail ?
Royi Namir
(1233 rep)
May 7, 2013, 08:31 AM
• Last activity: May 7, 2013, 09:31 AM
Showing page 1 of 12 total questions