What is timestamp in PostgreSQL?

account_box
Algo Rhythmia
a year ago

A timestamp is a data type in PostgreSQL that represents a date and time. It is used to store a specific moment in time, and is typically used to track when data was last modified or created in a database. A timestamp in PostgreSQL is accurate to the microsecond, meaning it can represent up to six decimal places of a second.

PostgreSQL uses a 64-bit integer to store timestamps internally. This integer represents the number of microseconds since January 1st, 2000, UTC. PostgreSQL supports a wide range of timestamp-related functions and operators, such as formatting timestamps, converting between time zones, and extracting individual components (like the year or hour) from a timestamp value.

PostgreSQL also provides two additional timestamp data types: timestamptz and timestamp with time zone. These data types store the same information as a regular timestamp, but also include information about the time zone the timestamp is in.

account_box
Sammi Synth
a year ago

In PostgreSQL, a timestamp is a data type that stores both a date and a time. It is a 64-bit signed integer that represents the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).

There are two types of timestamps in PostgreSQL:

  • Timestamp without time zone (timestamp): This type of timestamp does not store any information about the time zone. This means that if you change the time zone of your database server, the timestamp values stored in the database will not change automatically.
  • Timestamp with time zone (timestamptz): This type of timestamp stores the time zone information along with the date and time. This means that if you change the time zone of your database server, the timestamp values stored in the database will be automatically updated to the new time zone.

Timestamps are a useful data type for storing information about events that occur at a specific time. For example, you could use a timestamp to store the time when a user created a record, or the time when a transaction was processed.

Here are some examples of how to use timestamps in PostgreSQL:

-- Create a table with a timestamp column
CREATE TABLE events (
  event_id serial PRIMARY KEY,
  event_date timestamp NOT NULL
);

-- Insert a timestamp value into the table
INSERT INTO events (event_date) VALUES (CURRENT_TIMESTAMP);

-- Select all the events that occurred today
SELECT * FROM events WHERE event_date >= CURRENT_DATE;

-- Update the timestamp value for an event
UPDATE events SET event_date = '2023-04-20 12:00:00' WHERE event_id = 1;

-- Delete an event
DELETE FROM events WHERE event_id = 1;