Working with Time Zones using AT TIME ZONE in PostgreSQL
📖 Scenario: You are managing an international event scheduling database. Events are stored with timestamps in UTC, but users want to see event times in their local time zones.
🎯 Goal: Build a simple PostgreSQL query that converts UTC timestamps to a specified local time zone using the AT TIME ZONE clause.
📋 What You'll Learn
Create a table called
events with columns event_id (integer) and event_time_utc (timestamp without time zone).Insert three events with exact UTC timestamps: '2024-06-01 12:00:00', '2024-06-01 18:30:00', and '2024-06-02 09:15:00'.
Create a variable or CTE named
target_zone with the value 'America/New_York'.Write a SELECT query that converts
event_time_utc to the target_zone time zone using AT TIME ZONE and returns event_id and the converted time as event_time_local.💡 Why This Matters
🌍 Real World
Event scheduling systems often store times in UTC but need to display them in users' local time zones for clarity.
💼 Career
Understanding time zone conversions is essential for database developers and analysts working with global data.
Progress0 / 4 steps