---
title: Tips for handling timezones in Postgres
url: "https://aaronfrancis.com/videos/tips-for-handling-timezones-in-postgres"
path: "/videos/tips-for-handling-timezones-in-postgres"
type: video
author: Aaron Francis
published: 2024-09-27
topic: Databases
keywords: [postgres timezones, utc, postgres, timezone best practices, at time zone, session timezone, named timezones, timezone abbreviations, posix timezone, timezone offsets, timestamp conversion, local time zones]
youtube_url: "https://www.youtube.com/watch?v=oWF5jTFBSHw"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Tips for handling timezones in Postgres

## Chapters

- Introduction to Time Zones in PostgreSQL
- Why Time Zones Are Frustrating
- Best Practices for Handling Time Zones
- Using UTC for Consistency
- Setting the Time Zone for Your Database and Session
- Impact of Session Time Zone on Timestamps
- How PostgreSQL Stores and Displays Timestamps
- Converting UTC to Local Time Zones
- The Importance of Using Named Time Zones
- Using at time zone for Conversion
- Potential Pitfalls of Using Abbreviations (CST/CDT)
- The Problem with Hour Offsets
- Understanding Posix Style Time Zone Specifications
- Avoiding Mistakes with Time Zone Offsets
- Listing and Selecting Time Zone Names in PostgreSQL
- Final Recommendations for Handling Time Zones

## Transcript

**Aaron Francis:** Hey, what you're about to see is a video about one of the most surprising and extremely frustrating parts of the way that PostgreSQL handles some time zones. This is a video from the course Mastering PostgreSQL, which you can find at masteringpostgresql.com. It's a course where I teach you how to level up your career by mastering the most important part of your tech stack, your PostgreSQL database.

**Aaron Francis:** You guessed it. So I just wanted to give you a taste of what the course is like, and you can head over to masteringpostgresql.com if you want more. Before you do, I wanna give a shout-out to Xata at xata.io. They are a production grade, rock solid PostgreSQL platform for teams that ship fast. So if you need a place to put your PostgreSQL database, check out Xata.

**Aaron Francis:** Okay. Let's watch this video about the way that PostgreSQL handles time zones. Okay. Brace yourself, 'cause we're gonna talk about time zones, so take a deep breath, find your center, get your tea or whatever you need to do to relax. It's going to be fine, but, but time zones are very frustrating. Um, one of the reasons that time zones are frustrating is because they're political, not mathematical.

**Aaron Francis:** So we might say, "Great, we'll just move it six hours over." Well, are you sure about that? Is it currently daylight saving time, and so are you gonna mess up? Is the, is it currently daylight saving time, but the date that you're storing is, in fact, not in daylight saving time? That gets really frustrating, and what's even worse is, um, the time zone identifiers.

**Aaron Francis:** If you simply use offsets, sometimes they're positive, sometimes they're negative. So here's gonna... Here's, here's my, um, overarching recommendation when it comes to time zones. Keep it in UTC for as long as possible. Keep it in UTC for as long as possible. Convert it to whatever the user needs at the latest moment possible.

**Aaron Francis:** That is my recommendation for sanity. My second recommendation for sanity is use named time zones, not, uh, offsets, not hour offsets. And so I'll show you that as well, and I'll show you where you can get totally hosed if you use an hour offset accidentally. You'll end up actually going the opposite direction of what you're trying to do.

**Aaron Francis:** So keep it in UTC. Let's use named time zones. Let me show you. Keeping everything in UTC is a matter of making sure that your time zones are set to UTC. So we see show timezone here is set to UTC. We can set timezone equal to America/Chicago. I'm, I'm not from Chicago. That's my Chicago accent.

**Aaron Francis:** I'm from Dallas. Um, and then we were to show timezone again. Now we're set to America/Chicago, but that is for this session, and so you can set that. Um, you can set that per session. You can set that in your, uh, psqlrc file. Wherever you wanna set your session, you can, but if we were to disconnect and reconnect, it would reset to the database time zone or the, the cluster time zone.

**Aaron Francis:** And to change that, we have two options. Um, so we can say alter table... Nope. Alter database demo set timezone to UTC. So that is one way that you can explicitly change your database to the correct time zone, what, in my opinion, is the correct time zone.

**Aaron Francis:** Um, if you don't want to do that, you can change it at the, um, at the cluster level by modifying your PostgreSQL configuration file. You will have to reload the configuration file after you're done, and importantly, if a database has declared its own, um, discrete time zone, then this default will not apply.

**Aaron Francis:** So the, um, PostgreSQL configuration file is the default, and then you have the database on top of that, and then you have your session. And so why... I'm gonna set my session back to, um... Let's do it down here. I'm gonna set my session back to UTC, so we're UTC there, UTC there. So why is this so important? Remember when we talked about timestamps and timestamps with time zones, that when a, um, a new value comes in for a column that is a timestamp with timezone, PostgreSQL

**Aaron Francis:** is going to convert that to UTC for storage and then convert that back to whatever the time zone is for display. That conversion takes place if your, um, database or your session is not set to UTC. Let me see if we can prove that. We're gonna select this literal again, and we're gonna cast it to a timestamp tz.

**Aaron Francis:** So if we do that, you see it has been given a time zone. It has been given a time zone of +00. Where did that time zone come from? You got it. It came from our timezone setting. So the first thing, the first thing that we need to know about this is if you pass in a value or a literal that doesn't have a time zone and you're saying, "Please put this in a timeston- time zone aware column," it's gonna pick the default.

**Aaron Francis:** Now, if we were to say, in fact, this is a +0000, that still works just fine, but now watch if we were to change this to America/Chicago. And so it's 11:30, and so we're gonna change that. It's 11:30. Now it's 5:30. So what happened?

**Aaron Francis:** What happened is, um, this is a legit, this is a legit time at UTC, but when it was cast to a timestamp tz coming on the way back out, PostgreSQL looked and said, "Aha. You exist in America/Chicago, so I'm gonna take this, and I'm going to shift it six hours to account for the fact that you, me, Aaron, am, am in America/Chicago and my setting is America/Chicago," and

**Aaron Francis:** so it converted it for me. That may be incredibly useful. That might be exactly what you want. That is ha-Hardly ever what I want. What I want most of the time is I just want you to give me back UTC. I just want you to give me back UTC, and then maybe later in the application, I will work with it.

**Aaron Francis:** Now, what happens when you wanna convert a UTC timestamp to an actual local timestamp for displaying? You can totally do that. You don't have to do that on the application side, and you don't have to change the time zone of your session to do that. We have affordances for that. But before I show you, I wanna give you one giant, huge caveat. Please, if you're, if you're backgrounding me, that's fine.

**Aaron Francis:** Pay attention to this part. Use a named time zone. Use one of the time zones that is named for two reasons. One, it's gonna cover daylight saving time, and two, it's not gonna absolutely shoot you in the foot when you accidentally flip a sign. Let me show you what I mean by the flipping of the sign. Let's confirm we are still UTC show time zone.

**Aaron Francis:** We're still UTC. So if we select that, you see we still have a plus 00. We're casting it to a timestamp TZ. I feel like we're in a good spot to start. So we're gonna leave that one alone, and then we're gonna say at timezone America/Chicago. And if we run that, you'll see, um, that's a six-hour difference.

**Aaron Francis:** That's correct. That is correct to me. That is, um, that is what we are looking for. It has converted it. Importantly, it has lost the time zone identifier. So if we did a PG type of, and we ran that, you'll see it's a, it is a timestamp without time zone. So this, I think, should be the very end of your pipeline where you're just displaying it out to the user.

**Aaron Francis:** You're no longer doing date math or operations on it or anything like that. We have converted it to a local America Chicago timestamp. Now, let's carry on and do this again, but instead say CDT. I think CDT is right. Um, no, CST, Standard Time. So there you go. That's part of it.

**Aaron Francis:** I mean, um, whether or not you're in daylight saving time is really gonna muck you up if you do it wrong. Um, let's give these some names. So we'll say as, uh, base, as, um, America/Chicago, as CST, and as CDT. Okay, so the base, uh, why did I call that base?

**Aaron Francis:** Obvious. That was right in front of me. The UTC is 11:30. America/Chicago is five. CST is five. CDT is six. So provided you're always picking the exact right one between CST and CDT, you'll be fine. If you use the named, uh, time zone, you're gonna be much better off.

**Aaron Francis:** Now, you can use the hour offsets, but what you're going to find may surprise you. Um, time zone, we are negative six, um, so we're a six-hour offset from UTC. This is going to blow your mind.

**Aaron Francis:** Seventeen. Seventeen, not five, not even six. Six is wrong, but it's directionally correct. Um, five is the correct answer. Five o'clock in the morning. Six o'clock in the morning is close, but off by one. This is off by twelve. Right? Isn't that twelve?

**Aaron Francis:** That's off the wrong direction. If I were to change this to a plus six, you'll see, hey, we're right again. But I am certain, because I've looked so many times, that the central time zone is a negative six. This is the problem. This is the problem, um, using hour offsets.

**Aaron Francis:** Watch this again. Um, hour offset. Let's do, let's do an interval offset. And we'll say interval. And there, we're back to where we want to be. Five is correct. Six is close. Good try. Five is correct. Five is correct. Seventeen, super-duper wrong.

**Aaron Francis:** This is incredibly frustrating, I will admit. So here's what's happening. I'll just cut to the chase here. The Postgres docs, which I have scoured to no end, the Postgres docs declare three different ways you can specify a time zone. The first is a full time zone name like I've been recommending, America//Chicago.

**Aaron Francis:** That is the first way that you can do it. The second way is the time zone abbreviation. That's the CST, CDT. That's fine. You might have to handle daylight saving time yourself. That's not ideal, but it's okay. The third way listed in the docs, I'm, I'm gonna read this for you. In addition to time zone names and abbreviations, Postgres will accept POSIX-style time zone specifications.

**Aaron Francis:** This option is not preferable, uh, but may be necessary if the time zone entry is not available. So importantly, importantly, it says it may, um, it-- you can use a POSIX-style time zone specification. On a very separate page of the documentation, it says what a POSIX-style time zone definition is, and it has this footnote here.

**Aaron Francis:** The positive sign is used for zones west of Greenwich, parentheses. Note that this is the opposite of ISO 8601 sign convention used elsewhere in Postgres. This is the opposite of ISO 8601 used elsewhere. So if we go back to, yeah, let's come back here.

**Aaron Francis:** If we go back to this, what is happening here is this is the first version that the docs recommend, fully named, awesome, love it. This is the second version, abbreviation, fine if you have to. This is the third version, which is interpreted as a POSIX style instead of ISO 8601 style, so in fact, it is the exact opposite of what you're looking for.

**Aaron Francis:** And because we're moving six hours in either direction, that's where we got our 12-hour difference of being wrong. If for whatever reason you must use an hour offset, use this interval in the middle. Save what sanity you have left after fighting with all of this. Do not use a bare hour offset like this because you're gonna be wrong in, uh, it's gonna be two.

**Aaron Francis:** It's gonna be wrong in, instead of moving left, you're gonna move right, so your order of magnitude there is going to be quite large. Frustrating, I know. Here's how you can get a list of all of the time zone names. So if you want, you can just see what you've got going on there. You see lots of good ones, um, so many good ones, 597.

**Aaron Francis:** And if you wanna narrow it down, you can see like Chicago, and you see abbreviation currently is CDT because we are existing in daylight saving time, which is a UTC offset of negative five instead of negative six, which honestly, this proves my point quite nicely. We looked at this time zone table, and we are, um, currently in DST.

**Aaron Francis:** We're in daylight saving time at the time of recording, which is correct. But that date, that January 31st date that we've been operating on exists in Central Standard Time, but I don't care. I really don't care as long as I use a named time zone identifier. So I'm gonna use America/Chicago, and so should you.

**Aaron Francis:** Hopefully, that will save you from a lot of pain down the road.

---

## Related on aaronfrancis.com

- [About](/about) *(Page · 2026-11-25 · shared: postgres)*
- [Database School is live, and AI got really good](/newsletter/database-school-is-live-and-ai-got-really-good) *(Newsletter · 2025-12-05 · shared: postgres)*
- [Take things seriously](/newsletter/take-things-seriously) *(Newsletter · 2025-11-21 · shared: postgres)*
- [Laravel New](/podcast/laravel-new-76ab1e52) *(Podcast · 2025-09-16 · shared: postgres)*
- [Why Most Developers Overcomplicate Everything w/ Aaron Francis](/podcast/why-most-developers-overcomplicate-everything-w-aaron-francis-855db0fe) *(Podcast · 2025-03-20 · shared: postgres)*

## Relevant Resources from Aaron Francis

- **[Mastering Postgres](https://databaseschool.com)** (overview on site: [/courses](/courses)): Deep-dive video course on PostgreSQL indexing, query performance, JSONB, full-text search, locking, and production operations.
- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `postgres timezones` ([O-P](/llms/keywords/o-p.txt)), `utc` ([T-Z](/llms/keywords/t-z.txt)), `at time zone` ([A-B](/llms/keywords/a-b.txt)), `session timezone` ([S](/llms/keywords/s.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
