Sunday, 21 January 2024

PostgreSQL: Understand TIMESTAMP vs TIMESTAMP WITH TIME ZONE

 In this post, I am sharing a practical difference between TIMESTAMP and TIMESTAMP WITH TIME ZONE data type of PostgreSQL.

To understand the difference is a very important. Otherwise, it will affect your business or dataset.

I found many people are using TIMESTAMP WITH TIME ZONE data time, without knowing that this data type will change the time value according to different TIME ZONEs.

TIMESTAMP: Never change time basis on time zones
TIMESTAMP WITH TIME ZONE: Change the time basis on time zones

Please check the below demonstration:

Create two sample tables:

Insert CURRENT_TIMESTAMP:

Check the current timezone of your system:

Check the table data, which entered as your system time zone:

Now, change the timezone in your session:

Now, check the table data:
You can see, for ABC table no change and XYZ table changed as per the new time zone.





No comments:

Post a Comment

Master and Slave - Sync check - PostgreSQL

  1) Run the below Query on Primary:- SELECT     pid,     usename,     application_name,     client_addr,     state,     sync_state,     sen...