Open In App

PostgreSQL – Timestamp Data Type

Last Updated : 17 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PostgreSQL supports two primary temporal data types to store date and time: TIMESTAMP (without timezone) and TIMESTAMPTZ (with timezone). Understanding these data types is crucial for managing date and time effectively across different regions and time zones.

In this article, we will explain the TIMESTAMP and TIMESTAMPTZ data types in detail, along with examples to help us manage date and time in our PostgreSQL database.

PostgreSQL Timestamp Data Types

The TIMESTAMPTZ datatype is particularly useful for applications that need to account for time zone differences across regions. Both data types use 8 bytes of storage. PostgreSQL provides two temporal data types for handling timestamps:

  • TIMESTAMP: This stores date and time without timezone data. The stored value remains the same even if the server’s time zone is changed.
  • TIMESTAMPTZ: This stores date and time along with time zone information. PostgreSQL automatically converts the timestamp to UTC for storage and adjusts it back based on the current time zone settings when queried.

Syntax

TIMESTAMP; or TIMESTAMPTZ;

Examples of PostgreSQL Timestamp Data Type

Now let’s look into some examples of Timestamp in PostgreSQL for better understanding. These examples will demonstrate how to handle time zones, store date and time values, and convert between different time zones using PostgreSQL’s built-in functions for better clarity and real-world application.

Example 1: Working with TIMESTAMP and TIMESTAMPTZ

In this example, we will create a table with both TIMESTAMP and TIMESTAMPTZ columns, set the time zone, insert data, and query it. We will observe how the values differ when stored and retrieved under different time zone settings.

Step 1: Create a Table

First we create a table that has both TIMESTAMP and TIMESTAMPTZ columns using the below command: 

CREATE TABLE timestamp_demo (
ts TIMESTAMP,
tstz TIMESTAMPTZ
);

Step 2: Set the Time Zone

Then we will set the time zone of database server to Asia/Calcutta as below: 

SET timezone = 'Asia/Calcutta';

Step 3: Insert Data

Now that our time zone is set, we will insert a new row into the ‘timestamp_demo’ table using the below command: 

INSERT INTO timestamp_demo (ts, tstz)
VALUES
( '2020-06-22 19:10:25-07', '2020-06-22 19:10:25-07' );

Step 4: Query Data

Now we will query data from the TIMESTAMP and TIMESTAMPTZ columns using the below command: 

SELECT ts, tstz
FROM timestamp_demo;

Output

Example1

Example1

Explanation:

The output shows the TIMESTAMP value without timezone and the TIMESTAMPTZ value adjusted to the Asia/Calcutta timezone.

Example 2: Time Zone Conversion

In this example we will convert Asia/Calcutta timezone into America/New_York timezone using the timezone(zone, timestamp) function. 

Step 1: Create a Table

First we create a table that has both timestamp and timestamptz columns using the below command: 

CREATE TABLE timezone_conversion_demo ( tstz TIMESTAMPTZ);

Step 2: Set the Time Zone

Then we will set the time zone of database server to Asia/Calcutta as below: 

SET timezone = 'Asia/Calcutta';

Step 3: Insert Data

Now that our time zone is set, we will insert a new row into the timezone_conversion_demo table using the below command: 

INSERT INTO timezone_conversion_demo ( tstz)
VALUES
( '2020-06-22 19:10:25-07' );

Step 4: Convert Time Zone

Now we will query data from the timestamp and timestamptz columns using the below command: 

SELECT timezone('America/New_York', '2020-06-22 19:10:25');

Output

Example2

Example2

Explanation:

The output shows the TIMESTAMPTZ value converted to the America/New_York timezone.

Important Points About Timestamp Data Type in PostgreSQL

  • TIMESTAMPTZ is recommended when working with different time zones to avoid inconsistencies in stored data.
  • TIMESTAMP is useful when time zone is not a concern, such as when storing event logs with a consistent time reference.
  • Use PostgreSQL’s built-in functions like timezone(zone, timestamp) for easy conversions between different time zones.
  • Both TIMESTAMP and TIMESTAMPTZ use 8 bytes of storage.

Conclusion

In conclusion, PostgreSQL timestamp functions like NOW() and CURRENT_TIMESTAMP allow us to efficiently manage date and time values in our applications. Using TIMESTAMPTZ for time zone-aware data is highly recommended for global applications. By understanding how to use PostgreSQL timestamp, we can ensure consistency across different regions and effectively handle time zone conversions for data stored in your PostgreSQL database.



Next Article

Similar Reads