September 202414:0211.3k views

Tips for handling timezones in Postgres

About

In this video, I'll show you how to keep your sanity by using best practices (aka the least frustrating way) to handle timezones in PostgreSQL!

Transcript

Everything I said

00:00

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.

00:23

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.

00:45

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.

01:07

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.

01:31

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.

01:57

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.

02:20

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.

02:40

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.

03:04

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.

03:25

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.

03:48

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

04:19

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.

04:42

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.

05:11

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?

05:32

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

06:03

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.

06:23

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.

06:44

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.

07:05

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.

07:27

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.

07:52

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.

08:13

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?

08:37

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.

08:57

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.

09:18

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?

09:39

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.

10:02

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.

10:25

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.

10:46

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.

11:15

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.

11:43

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.

12:04

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.

12:29

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.

12:53

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.

13:15

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.

13:37

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.

13:57

Hopefully, that will save you from a lot of pain down the road.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues