question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datetime stored incorrectly in postgresql

See original GitHub issue

Issue type:

[x ] bug report

Database system/driver:

[ x] postgres

TypeORM version:

[ ] 0.2.6 (or put your version here)

Steps to reproduce or a small repository showing the problem:

The entity and test call below result in a timestamp in server time(rather than UTC) and without timezone information. This behavior exists as of pull request #1717 as far as I can tell.

@entity() export class DateTest {

@PrimaryGeneratedColumn(“uuid”) public id?: string;

@Column() public date?: Date; }

testRepository.save({date: new Date()});

A potentially dangerous result of this bug is that two different servers in different timezones, but accessing the same database, could read the same entry and construct different timestamps(not just differences in timezone).

The comment here by @chriskalmar is not the actual behavior for postgres. As shown in the example above, new Date() is not stored as UTC.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

34reactions
pleerockcommented, May 27, 2018

Why is your server in different timezones? You shall use UTC in any server you have to avoid issues. Try to set process.env.TZ = "UTC"; in your app

30reactions
filipjnccommented, May 27, 2018

I had the same problem (also postgres) and I fixed it with the following attribute for all date columns:

  @UpdateDateColumn({ type: 'timestamptz' })
  updatedAt: Date;

Have a look at these supported column types by TypeORM: http://typeorm.io/#/entities/column-types-for-postgres

Update: timestamptz is just an abbreviation of the timestamp with time zone mentioned above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect date fetched from Postgres - java
Postgres always stores a moment in UTC in a column of type TIMESTAMP WITH TIME ZONE . ... Beware of your database access...
Read more >
Time stored using now()::timestamp function stores wrong ...
to store date and time without time zone. You are using the wrong data type when defining your column. The TIMESTAMP WITHOUT TIME...
Read more >
Documentation: 15: 8.5. Date/Time Types
PostgreSQL supports the full set of SQL date and time types, shown in Table 8.9. The operations available on these data types are...
Read more >
Wrong displays of DateTime fields from PostgreSQL to ...
Therefore, all columns display wrong dates regardless of the time zone selected. Any of this is an issue when using PostgreSQL, but it...
Read more >
PostgreSQL Convert DateTime to Date: 10 Practical ...
DateTime variables store important information, such as the date, time, and timezone for when a transaction took place or when a new entry ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found