Home > other >  How do I store Utc timestamp in postgres by default?
How do I store Utc timestamp in postgres by default?

Time:01-30

I know there was a similar question posted, but it is 5 years old.

I'm creating a table in postgres with a field created_at defined like this:

created_at TIMESTAMP WITH TIME ZONE DEFAULT (now() at time zone('utc'))

For some reason actual datetime value stored in the database has local timezone offset.

How do I make postgres store a timestamp with an actual Utc offset? Thank you

CodePudding user response:

CREATE TABLE example_table (
  id serial primary key,
  created_at timestamptz default now()
);

In this example, the created_at column will automatically store the current UTC time whenever a new row is inserted into the table.

CodePudding user response:

My code from the question worked as well. The issue was kind of stupid: I'm using DBeaver to inspect my database and apparently DBeaver converts UTC time into local time when it displayed to the user.

  • Related