Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing timezones in postgres

I need to store timezones for users in Postgres 9.3.

A logical solution would be to create a table of timezone names and offsets, and then store an FK reference in my user table.

However, postgres already has a view pg_timezone_names which displays the timezone names Postgres knows about. It would seem to make sense just to reference those names, rather than creating my own table of the exact same names. However, pg_timezone_names is a view and therefore can't be referenced.

Is there a system table holding these timezone names which is safe to reference, or is it simply a bad idea to do so?

like image 215
wadesworld Avatar asked Apr 12 '26 04:04

wadesworld


1 Answers

Rather than a FK reference, you should probably just denormalize. Store the string name of the time zone with the user, using an IANA time zone identifier, such as 'America/Los_Angeles'. These IDs are used by Postgres, and are also interchangeable with other systems.

Keep in mind that a time zone cannot be solely represented by an offset. Read more in the section "Time Zone != Offset" of the timezone tag wiki.

like image 188
Matt Johnson-Pint Avatar answered Apr 15 '26 01:04

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!