Last Updated : 15 Jul, 2025
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 TypesThe 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:
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 TIMESTAMPTZIn 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 TableFirst 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
Example1Explanation:
The output shows the TIMESTAMP
value without timezone and the TIMESTAMPTZ
value adjusted to the Asia/Calcutta
timezone.
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
Example2Explanation:
The output shows the TIMESTAMPTZ
value converted to the America/New_York
timezone.
timezone(zone, timestamp)
for easy conversions between different time zones.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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4