PostgreSQL – Timestamp Data Type
Last Updated :
17 Oct, 2024
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
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
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.
Similar Reads
PostgreSQL - TIME Data Type
In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQLâs TIME data type also supports fractional seconds for u
4 min read
PostgreSQL - TEXT Data Type
PostgreSQL provides a highly flexible character data type known as TEXT, designed to store character strings of virtually unlimited length. Unlike the VARCHAR data type, which can be limited to a specified length, the TEXT data type offers the same efficiency and performance without the length const
3 min read
PostgreSQL - Data Types
PostgreSQL is a robust open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column, affecting storage, access, and manipulation. In this article, We will learn about the P
5 min read
PostgreSQL TO_TIMESTAMP() Function
In PostgreSQL, managing and manipulating date and time values is important, especially when they are stored as strings. The to_timestamp function allows us to convert textual representations of dates and times into a valid timestamp format and making it easier to work with them in queries, calculati
5 min read
PostgreSQL - Date Data Type
PostgreSQL offers powerful DATE data type and date functions to efficiently handle date and time information. PostgreSQL DATE data type allows for storing and manipulating calendar dates while its robust set of date functions enables users to perform operations like date arithmetic and formatting. I
4 min read
PostgreSQL - CHAR Data Type
The CHAR data type in PostgreSQL is one of the essential character data types for storing fixed-length strings. Unlike VARCHAR, which stores variable-length data, CHAR is used when we need to store a fixed-length string. This article will explain the CHAR data type in PostgreSQL, its syntax, common
5 min read
DATETIME vs TIMESTAMP Data Type in MySQL?
When designing a database schema in MySQL, choosing the appropriate data type for storing date and time information is important. MySQL offers two primary data types for this purpose DATETIME and TIMESTAMP. Each has its characteristics and use cases and understanding these can help us make the right
6 min read
PostgreSQL - Array Data Type
PostgreSQL provides an advanced and flexible feature known as the Array Data Type, which allows us to store multiple values in a single column. Arrays in PostgreSQL can be used with all built-in data types and even user-defined types, enabling a wide range of use cases. In this article, we will expl
4 min read
PostgreSQL - INTEGER Data Type
In PostgreSQL, the INTEGER data type is widely used for storing numerical data efficiently. It is a 4-byte data type that allows us to store whole numbers within a specified range, making it ideal for various use cases like population counts, active user statistics, and more. In this article, we wil
4 min read
PostgreSQL - SMALLINT Integer Data Type
In PostgreSQL, the SMALLINT data type is a compact, efficient way to store integer values within a small range. Using only 2 bytes of storage, SMALLINT is ideal for scenarios where the range of possible values is relatively small, such as the age of individuals or the number of pages in a book. In t
4 min read