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
Example1Explanation:
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
Example2Explanation:
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 - Data Types PostgreSQL is a powerful, 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. which allows developers to define, store, and manipulate data in a way that aligns w
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
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 - NUMERIC Data Type In PostgreSQL, the NUMERIC data type is designed for high-precision number storage by making it ideal for financial and scientific applications where accuracy is critical. It supports a large number of digits both before and after the decimal point, minimizing rounding errors. Understanding the nuan
5 min read
How to Extract Date From a TimeStamp in PostgreSQL PostgreSQL is a powerful open-source relational database management system (RDBMS). PostgreSQL is well-known for its feature-rich capabilities, standardization, and adaptability. It supports a variety of data types, complex SQL queries, and ACID properties. PostgreSQL offers scalability and durabili
4 min read
PostgreSQL - Interval Data Type The interval data type in PostgreSQL stores time periods using 16 bytes of storage and supports a range from -178,000,000 years to 178,000,000 years. It provides a precision attribute ('p') that allows you to specify the number of fractional digits retained in the seconds field, enhancing the precis
2 min read
PostgreSQL - CURRENT_TIMESTAMP Function PostgreSQL provides the 'CURRENT_TIMESTAMP() function, which returns the current date and time, along with the time zone, at the precise moment the transaction begins. This function is often used for tracking the creation or modification of records in a database.Here, weâll look into the syntax, usa
2 min read
SQL Data Types SQL Data Types are very important in relational databases. It ensures that data is stored efficiently and accurately. Data types define the type of value a column can hold, such as numbers, text, or dates. Understanding SQL Data Types is critical for database administrators, developers, and data ana
5 min read