upleveled / system-setup

Set up a PERN development environment on Windows, macOS and Linux
28 stars 19 forks source link

Configure PostgreSQL time zone to UTC #88

Closed karlhorky closed 4 months ago

karlhorky commented 4 months ago

Time zone inconsistencies between systems (eg. development environment and production environment) can be very difficult to debug.

Instead, set PostgreSQL to use UTC time (which seems to be best practice for deployment infrastructure, from a quick search)

TODO:

Useful commands:

# psql query to show database time zone
SHOW TIMEZONE;

# Show all lines in postgresql.conf
cat $PGDATA/postgresql.conf | grep timezone

# Show local system time zone
tzutil /g # Windows (unverified)
sudo systemsetup -gettimezone # macOS
timedatectl # Linux (unverified)

PostgreSQL v14

Note: The checks above should be done anyway, because we support latest PostgreSQL (currently PostgreSQL v16)

But just for documentation / posterity:

PostgreSQL v14 on macOS

On my local system (macOS PostgreSQL v14, via Homebrew), here's the output of SHOW TIMEZONE; before this config:

=> SHOW TIMEZONE;
   TimeZone
---------------
 Europe/Vienna
(1 row)

In my /opt/homebrew/var/postgresql@14/postgresql.conf file:

$ cat $PGDATA/postgresql.conf | grep timezone
log_timezone = 'Europe/Vienna'
timezone = 'Europe/Vienna'
#timezone_abbreviations = 'Default'     # Select the set of available time zone
                                        # share/timezonesets/.

My system time zone:

$ sudo systemsetup -gettimezone
Time Zone: Europe/Vienna

PostgreSQL v14 on Linux (Debian)

With psql connecting to a remote PostgreSQL v14 on a Linux (Debian) machine

=> SHOW TIMEZONE;
 TimeZone 
----------
 UTC
(1 row)

Because PostgreSQL v14 is installed remotely on another system which I don't have access to, I cannot check the postgresql.conf file or local system time zone on that machine.

ProchaLu commented 4 months ago

This is my output when testing on my machine:

spring2024=> SHOW TIMEZONE;
   TimeZone
---------------
 Europe/Vienna
(1 row)
➜  ~ sudo systemsetup -gettimezone

Password:
Time Zone: Europe/Vienna
➜  ~ cat $PGDATA/postgresql.conf | grep timezone
log_timezone = 'Europe/Vienna'
timezone = 'Europe/Vienna'
#timezone_abbreviations = 'Default'     # Select the set of available time zone
                    # share/timezonesets/.
Eprince-hub commented 4 months ago

macOS

Windows

System timezone

PowerShell: Get-TimeZone and Hyper: powershell -command "Get-TimeZone" produced the same result

// Hyper
Id                         : W. Europe Standard Time
DisplayName                : (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
StandardName               : W. Europe Standard Time
DaylightName               : W. Europe Summer Time
BaseUtcOffset              : 01:00:00
SupportsDaylightSavingTime : True

// PowerShell
Id                         : W. Europe Standard Time                                                                    
DisplayName                : (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna                               
StandardName               : W. Europe Standard Time                                                                    
DaylightName               : W. Europe Summer Time                                                                      
BaseUtcOffset              : 01:00:00                                                                                   
SupportsDaylightSavingTime : True   

Linux

System timezone running date

Mon Jul 29 11:08:30 UTC 2024

running cat /etc/timezone

Etc/UTC