Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set session timezone to local timezone on startup #1482

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Contributors:
* saucoide
* Chris Rose (offbyone/offby1)
* Chris Novakovic
* Max Smolin (maximsmol)

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Dev
Features
--------
* Add a `--ping` command line option; allows pgcli to replace `pg_isready`
* The session time zone setting is set to the system time zone by default

Bug fixes:
----------
Expand Down
3 changes: 3 additions & 0 deletions pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from cli_helpers.utils import strip_ansi
from .explain_output_formatter import ExplainOutputFormatter
import click
import tzlocal

try:
import setproctitle
Expand Down Expand Up @@ -1624,6 +1625,8 @@ def cli(
else:
pgcli.connect(database, host, user, port)

pgcli.pgexecute.set_timezone(tzlocal.get_localzone_name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ever raise an exception or return a None?
What happens if I set TZ to a non-existent timezone name?

Copy link
Author

@maximsmol maximsmol Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • It does not throw if TZ is an invalid value (I also tested this directly)
  • It would throw if multiple conflicting timezone configurations are found in one of the above files. I added a try-catch to print a warning and continue without setting a custom timezone (using the default server settings instead)
  • It might return None if no timezone configuration is found. I added a check that prints a warning and continues without setting a timezone

For due diligence, from reading the source code:

  • It will throw if TZ points to a file (absolute path) and os.path.exists or realpath return throw an error
  • It could throw if exists("/system/bin/getprop") throws
  • It could throw if exists, islink or realpath throws for /etc/localtime
  • It could throw if realpath throws for a timezone config referred to from /etc/timezone, /var/db/zoneinfo, /etc/sysconfig/clock, /etc/conf.d/clock, or /etc/localtime


if list_databases:
cur, headers, status = pgcli.pgexecute.full_databases()

Expand Down
7 changes: 7 additions & 0 deletions pgcli/pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,10 @@ def casing(self):

def explain_prefix(self):
return "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) "

def set_timezone(self, timezone: str):
query = psycopg.sql.SQL("set time zone {}").format(
psycopg.sql.Identifier(timezone)
)
with self.conn.cursor() as cur:
cur.execute(query)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"sqlparse >=0.3.0,<0.6",
"configobj >= 5.0.6",
"cli_helpers[styles] >= 2.2.1",
"tzlocal >= 5.2",
]


Expand Down