Wrong timestamp!
var pg = require('pg'); var conString = "..."; var client = new pg.Client(conString); client.connect(function(err) { if(err) { return console.error('could not connect to postgres', err); } var sql = "SELECT NOW() as now1, timezone('UTC'::text, now()) as now2"; client.query(sql, function(err, result) { if(err) { return console.error('error running query', err); } console.log(result.rows); client.end(); }); });
Result is: [ { now1: Thu Aug 29 2013 11:37:14 GMT+0300 (FLE Summer Time), now2: Thu Aug 29 2013 08:37:14 GMT+0300 (FLE Summer Time) } ] And must be: [ { now1: Thu Aug 29 2013 11:37:14 GMT+0300 (FLE Summer Time), now2: Thu Aug 29 2013 08:37:14 GMT+0000 (UTC) } ] or equal dates This is tested with latest version node-postgres.
i will post here in remembrance
Had to change my postgres column from timestamp to timestamptz.
This allows me to read the timestamps from database correctly when they are stored on the database in UTC time like documented in the official postgres documentation:
pg.types.setTypeParser(1114, function(stringValue) {
return new Date(Date.parse(stringValue + "+0000"));
});
But when I insert data to the database, it gets stored in local time and not in UTC time which is against the official postgres documentation:
const query:pg.QueryConfig = {
text: "INSERT INTO x(y);",
values: [new Date()],
};
How can I resolve this issue?
Use timestamptz instead of timestamp, at least on the app - db boundary.
Eg INSERT INTO t (ts) VALUES ($1 AT TIME ZONE 'UTC');
If your input can be parsed as timestamp with time zone, it will correctly be converted into an utc timestamp.
The other direction:
SELECT ts AT TIME ZONE 'UTC' FROM t;
Thanks for your reply boromisp. I was hoping there would be a more elegant way that makes node-postgres send the timestamp in UTC time instead of sending it in local time and then asking postgres to convert it to UTC time.
I can't comprehend how this default functionality can be considered to be usable. If I run this in my laptop in London, insert couple of timestamps, fly to New York, insert couple more timestamps and then go to Los Angeles to read the data, there isn't a way to interpret the timestamps correctly. Or if I have two servers in two different timezones using the same database. Or if the hosting platform is switched to an another one with different timezone. All the stored timestamps are just unusable garbage.
@byterator If you’re trying to store data that represents actual points in time, you should use timestamptz (it’s not actually a time-zone-aware type), and you don’t really need to care what it looks like in transport at that point (though it should be UTC eventually too, because there’s no reason for it not to be). pg’s default behaviour of treating timestamp without time zone and date as parseable to Date objects (that can be meaningfully manipulated as more than just a container for some UTC fields) is incorrect and only there for backwards compatibility.
INSERT INTO x (y AT TIME ZONE 'UTC'); should work in the meantime, if you can’t convert your columns right away. Or something with toISOString.
Thanks @charmander Converting my columns to timestampz seems to be the best solution even through it feels quite unnecessary to store the timezone information. Has marking of the current functionality as deprecated and developing alternative functionality been considered?
It's just confusing naming in postgres, timestamptz doesn't actually store time zone information. It's the type that represents an instant in time, probably stored in UTC.
Stored in UTC, but converted to database-local time on output. So one has to be careful of what time the database is in.
For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's TimeZone parameter, and is converted to UTC using the offset for the timezone zone.
When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.4).
But since the timestamptz output always contains the time zone offset, it doesn't really matter which time zone it's in. You just have to use a parser that understands time zone offsets, which the default parser of this library does.
Right -- vg :)
Hello friends, this really is a very complex and boring subject.
@brianc
Thanks to this discussion, I found a way out, but please clear one of my doubts! Postgres doesn't handle timezone without timezone? Doesn't the timestampz already exist for this?
Why doesn't the driver implement the solution proposed here by default in its implementation?
require('pg').types.setTypeParser(
1114,
(value: string) => new Date(Date.parse(`${value}+0000`)),
);
I don't know if what I said makes sense, but it seems ideal. It is not?