Time zones help us understand what time it is in different parts of the world. The AT TIME ZONE command changes the time to a specific time zone so we can see it correctly.
Time zones and AT TIME ZONE in PostgreSQL
timestamp_value AT TIME ZONE zone_name
timestamp_value can be a timestamp with or without time zone.
zone_name is a string like 'UTC', 'America/New_York', or an offset like '+02'.
TIMESTAMP '2024-06-01 12:00:00' AT TIME ZONE 'UTC'
TIMESTAMP WITH TIME ZONE '2024-06-01 12:00:00+00' AT TIME ZONE 'America/New_York'
TIMESTAMP '2024-06-01 12:00:00' AT TIME ZONE '+02'
This query shows how to convert a timestamp without time zone to UTC and New York time (both as timestamp with time zone), and how to convert a timestamp with time zone from UTC to New York time (returns timestamp without time zone).
SELECT TIMESTAMP '2024-06-01 12:00:00' AT TIME ZONE 'UTC' AS utc_time, TIMESTAMP '2024-06-01 12:00:00' AT TIME ZONE 'America/New_York' AS ny_time, TIMESTAMP WITH TIME ZONE '2024-06-01 12:00:00+00' AT TIME ZONE 'America/New_York' AS ny_time_from_tz;
When you use AT TIME ZONE on a timestamp without time zone, it treats the timestamp as if it is in the given zone and converts it to a timestamp with time zone in UTC.
When you use AT TIME ZONE on a timestamp with time zone, it converts it to the given zone but returns a timestamp without time zone.
Time zones like 'America/New_York' adjust for daylight saving time automatically.
Time zones help show correct local times worldwide.
AT TIME ZONE converts timestamps between zones.
Use it to display or compare times correctly across regions.
Practice
AT TIME ZONE clause do in PostgreSQL?Solution
Step 1: Understand the purpose of
This clause is used to convert timestamps between different time zones.AT TIME ZONEStep 2: Compare options
Only It converts a timestamp from one time zone to another. correctly describes this conversion. Other options describe unrelated actions.Final Answer:
It converts a timestamp from one time zone to another. -> Option AQuick Check:
AT TIME ZONEconverts time zones [OK]
AT TIME ZONE changes time zones, not system settings [OK]- Thinking it changes system time zone permanently
- Confusing it with formatting functions
- Assuming it removes time zone info
Solution
Step 1: Recall correct syntax for
The correct syntax uses two words:AT TIME ZONEAT TIME ZONEfollowed by the time zone in single quotes.Step 2: Check each option
Only SELECT timestamp_column AT TIME ZONE 'America/New_York'; matches the correct syntax exactly. The others either use AT TIMEZONE as one word (incorrect spelling) or miss quotes around the time zone name.Final Answer:
SELECT timestamp_column AT TIME ZONE 'America/New_York'; -> Option BQuick Check:
UseAT TIME ZONE 'zone'with quotes [OK]
- Writing AT TIMEZONE as one word
- Omitting quotes around time zone
- Using incorrect keyword order
SELECT '2024-06-01 12:00:00'::timestamp AT TIME ZONE 'UTC' AT TIME ZONE 'America/Los_Angeles';
What is the output timestamp?
Solution
Step 1: Understand the query steps
The firstAT TIME ZONE 'UTC'converts the timestamp without time zone to UTC timestamp with time zone. The secondAT TIME ZONE 'America/Los_Angeles'converts that UTC time to Los Angeles local time.Step 2: Calculate time difference
On June 1, Los Angeles is on PDT (UTC-7). So 12:00 UTC minus 7 hours is 05:00 local time. But since the query appliesAT TIME ZONEtwice, the final output is a timestamp without time zone in Los Angeles time, which is 05:00.Final Answer:
2024-06-01 05:00:00 -> Option CQuick Check:
UTC 12:00 minus 7 hours = 05:00 [OK]
- Confusing the order of AT TIME ZONE conversions
- Ignoring daylight saving time offset
- Assuming output is in UTC
SELECT '2024-06-01 12:00:00'::timestamp AT TIMEZONE 'Europe/London';
Solution
Step 1: Check the syntax of
The query usesAT TIME ZONEAT TIMEZONEas one word, which is incorrect syntax in PostgreSQL.Step 2: Verify other parts
The timestamp literal is valid, and 'Europe/London' is a valid time zone name. So no error there.Final Answer:
The keyword should beAT TIME ZONEwith a space. -> Option DQuick Check:
Use two words: AT TIME ZONE [OK]
- Writing AT TIMEZONE as one word
- Assuming timestamp needs time zone info
- Thinking time zone name is invalid
created_at stored in UTC. You want to display it as local time in 'Asia/Tokyo' but keep it as timestamp without time zone. Which query correctly does this?Solution
Step 1: Understand data type and conversion
created_atis timestamp with time zone in UTC. ApplyingAT TIME ZONE 'Asia/Tokyo'converts it to timestamp without time zone in Tokyo local time.Step 2: Analyze options
SELECT created_at AT TIME ZONE 'Asia/Tokyo' FROM orders; correctly appliesAT TIME ZONE 'Asia/Tokyo'to convert UTC timestamptz to local timestamp. SELECT created_at::timestamp AT TIME ZONE 'Asia/Tokyo' FROM orders; casts to timestamp first, losing time zone info before conversion, causing wrong result. The options that apply AT TIME ZONE 'UTC' first perform extra and incorrect conversions.Final Answer:
SELECT created_at AT TIME ZONE 'Asia/Tokyo' FROM orders; -> Option AQuick Check:
timestamptz AT TIME ZONE zone = local timestamp [OK]
- Casting timestamptz to timestamp before conversion
- Applying multiple AT TIME ZONE conversions unnecessarily
- Confusing timestamp with and without time zone
Ready to prove it?
Pass the test and this pattern moves unknown
