Last active
January 12, 2026 12:52
-
-
Save ivastly/f0f9c854782dcb8ef7c9e57e388d9a07 to your computer and use it in GitHub Desktop.
timestamptz_postgres_type_demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- this table has a `timestamp_no_tz` column (which allows bugs) and `timestamp_tz` column (which protects against bugs) | |
| create table tz_type_example | |
| ( | |
| timestamp_no_tz timestamp(0) not null, | |
| timestamp_tz timestamptz(0) not null | |
| ); | |
| -- inserting the same timestamp in a human-readable format into both columns | |
| insert into tz_type_example values ('2026-01-01 10:00:00 +05:00', '2026-01-01 10:00:00 +05:00'); -- same as 2026-01-01 05:00:00 +00:00 | |
| select * from tz_type_example; | |
| -- When we select the timestamps back, the timestamp from the `timestamp_no_tz` column "lost" 5 hours (see the dump below) | |
| -- However, the timestamp from the "timestamp_tz" is correct. | |
| -- We could normalise the input on the application side to solve this. | |
| -- But using the `timestamptz` solves this once and for all future table interactions, which makes the application less error-prone. | |
| -- timestamp_no_tz,timestamp_tz | |
| -- 2026-01-01 10:00:00,2026-01-01 05:00:00 +00:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment