You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different results on Windows and Mac with timestamp columns #190
Hi, I'm seeing inconsistencies with how timestamp without time zone columns behave on Mac and Windows when Sys.getenv("TZ") is different than Sys.timezone(). Tested on version 1.1.1.
Here's a minimal script to reproduce:
library(DBI)
print(Sys.timezone()) # America/Los_Angeles
Sys.setenv(TZ="America/New_York") # another time zonedb<- dbConnect(RPostgres::Postgres(), dbname="rpostgres_test")
dbExecute(db, "DROP TABLE IF EXISTS events")
dbExecute(db, "CREATE TABLE events (created_at TIMESTAMP)")
dbExecute(db, "INSERT INTO events VALUES ('2018-01-01 12:30:00')")
rs<- dbSendQuery(db, "SELECT * FROM events")
records<- dbFetch(rs)
print(records$created_at)
On Mac (as well as with RPostgreSQL), this produces 2018-01-01 12:30:00 EST.
On Windows, the same code produces 2018-01-01 3:30:00 EST (when Sys.timezone() is America/Los_Angeles).
What seems to be happening is on Mac, 2018-01-01 12:30:00 in the database is read in the TZ time zone. On Windows, 2018-01-01 12:30:00 in the database is read in the Sys.timezone() time zone, then converted to the TZ time zone for display.
On Mac, it returns 2018-01-01 12:30:00 (no time zone). From what I recall from debugging, it returns the same on Windows (won't have access to a Windows VM for a few weeks to run it again).
This might be related. Writing a table with timestamp column is failing when TZ env var is not set to Europe/London.
Adding Sys.setenv(TZ = Sys.timezone()) returned the same error when Sys.timezone() was Europe/Lisbon instead of Europe/London.
Error
Error in connection_copy_data(conn@ptr, sql, value) :
COPY returned error: ERROR: invalid input syntax for type timestamp: "2018-08-14 20:41:13 WEST"
CONTEXT: COPY table_name, line 1, column call_finished: "2018-08-14 20:41:13 WEST"
Calls: table_name ... dbWriteTable -> dbWriteTable -> .local -> connection_copy_data
RPostgres release
Tested with versions 1.1.1 and 1.1.1.9002
Session Info
Session info ------------------------------------------------------------------
setting value
version R version 3.5.1 (2018-07-02)
system x86_64, darwin15.6.0
ui X11
language (EN)
collate en_US.UTF-8
tz Europe/London
date 2018-10-31
Are you using EnterpriseDB PostgreSQL? On Windows, it has some locale defaults that aren't the same as what's traditional on Linux. You may need to re-install PostgreSQL and explicitly set the locale settings. 👎
I discovered this the hard way - by taking a .pgdump database save on a Windows EnterpriseDB install and attempting to restore it to a Linux install.
The way I understand timestamp without time zone, it just strips any time zone information from values. The Windows behavior seems wrong, need to try to replicate on Windows.
On Windows and with the current dev version of RPostgres, the timezone argument in dbConnect() is apparently ignored for timestamp without timezone. Instead, the Sys.timezone() offset is applied by dbGetQuery(), which it shouldn't.
library(DBI)
Sys.unsetenv("TZ") # environment variables from the main R session get carried through to reprex::reprex() https://community.rstudio.com/t/environment-variables-in-reprex-reprex/64499
Sys.timezone() # "Native" time zone#> [1] "Europe/Berlin"query<-"SELECT '2018-01-01 12:30:00'::TIMESTAMP"db<- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone="America/New_York")
dbGetQuery(db, query)[[1,1]]
#> [1] "2018-01-01 12:30:00 CET"
dbDisconnect(db)
db<- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone="Europe/London")
dbGetQuery(db, query)[[1,1]]
#> [1] "2018-01-01 12:30:00 CET"
dbDisconnect(db)
Sys.setenv(TZ="America/New_York") # another time zone (usually -6 relative to Europe/Berlin)
Sys.timezone()
#> [1] "America/New_York"db<- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone="America/New_York")
dbGetQuery(db, query)[[1,1]]
#> [1] "2018-01-01 06:30:00 EST"
dbDisconnect(db)
db<- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone="Europe/London")
dbGetQuery(db, query)[[1,1]]
#> [1] "2018-01-01 06:30:00 EST"
dbDisconnect(db)
Sys.unsetenv("TZ")
The difference between Windows and macOS was supposedly due to a different gmtime() implementation, this function is no longer used.
library(DBI)
# Must run in a new session on Linux
Sys.unsetenv("TZ") # environment variables from the main R session get carried through to reprex::reprex() https://community.rstudio.com/t/environment-variables-in-reprex-reprex/64499
Sys.timezone() # "Native" time zone#> [1] "Europe/Zurich"query<-"SELECT '2018-01-01 12:30:01.23'::TIMESTAMP"db<- dbConnect(RPostgres::Postgres(), timezone="America/New_York")
dbGetQuery(db, query)[[1]]
#> [1] "2018-01-01 12:30:01 EST"
dbDisconnect(db)
db<- dbConnect(RPostgres::Postgres(), timezone="Europe/London")
dbGetQuery(db, query)[[1]]
#> [1] "2018-01-01 12:30:01 GMT"
dbDisconnect(db)
db<- dbConnect(RPostgres::Postgres(), timezone="Europe/Zurich")
dbGetQuery(db, query)[[1]]
#> [1] "2018-01-01 12:30:01 CET"
dbDisconnect(db)
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.
Activity
krlmlr commentedon Aug 26, 2018
Thanks. What happens when you run these queries from
psql(or the equivalent on Windows)?ankane commentedon Aug 27, 2018
On Mac, it returns
2018-01-01 12:30:00(no time zone). From what I recall from debugging, it returns the same on Windows (won't have access to a Windows VM for a few weeks to run it again).quartin commentedon Oct 31, 2018
This might be related. Writing a table with timestamp column is failing when
TZenv var is not set toEurope/London.Adding
Sys.setenv(TZ = Sys.timezone())returned the same error whenSys.timezone()wasEurope/Lisboninstead ofEurope/London.Error
RPostgres release
Tested with versions 1.1.1 and 1.1.1.9002
Session Info
znmeb commentedon Oct 31, 2018
Are you using EnterpriseDB PostgreSQL? On Windows, it has some locale defaults that aren't the same as what's traditional on Linux. You may need to re-install PostgreSQL and explicitly set the locale settings. 👎
I discovered this the hard way - by taking a
.pgdumpdatabase save on a Windows EnterpriseDB install and attempting to restore it to a Linux install.krlmlr commentedon Nov 26, 2018
The way I understand
timestamp without time zone, it just strips any time zone information from values. The Windows behavior seems wrong, need to try to replicate on Windows.Relevant: https://dba.stackexchange.com/q/59006/6934 (but moot because many users of RPostgres aren't in control of the DB schema).
krlmlr commentedon Dec 17, 2019
Does the new
timezoneargument help here (available in 1.2.0, about to hit CRAN)?dpprdan commentedon May 4, 2020
On Windows and with the current dev version of RPostgres, the
timezoneargument indbConnect()is apparently ignored fortimestamp without timezone. Instead, theSys.timezone()offset is applied bydbGetQuery(), which it shouldn't.Session info
krlmlr commentedon Jan 3, 2021
Should be fixed with #276.
The difference between Windows and macOS was supposedly due to a different
gmtime()implementation, this function is no longer used.Created on 2021-01-03 by the reprex package (v0.3.0)
Created on 2021-01-03 by the reprex package (v0.3.0)
Merge pull request #276 from r-dbi/b-timezone
krlmlr commentedon Jan 3, 2021
Should be fixed now. Could you please test the development version in the next few days, and let me know if any problems still arise?
github-actions commentedon Jan 4, 2022
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.