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
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read