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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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())

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