I am struggling to understand what exactly happens behind the scenes when passing POSIXct
objects between R and Postgres using RPostgreSQL
. In the following example, I define two timestamp fields: one with a timezone the other one without. However, it appears that they are treated exactly the same when passing POSIXct
objects via dbWriteTable
and dbReadTable
.
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host = "127.0.0.1", port = "5432", user= "postgres",
dbname = "test_db")
q <- "
CREATE TABLE test_table
(
dttm timestamp without time zone,
dttmtz timestamp with time zone
)"
dbSendQuery(con, q)
# using timezone CET
dttm <- as.POSIXct("2016-01-01 10:20:10", tz="CET")
df <- data.frame(dttm = dttm, dttmtz = dttm)
dbWriteTable(con, "test_table", df, overwrite=FALSE, append=T, row.names=0)
# using timezone UTC
dttm <- as.POSIXct("2016-01-01 14:20:10", tz="UTC")
df <- data.frame(dttm = dttm, dttmtz = dttm)
dbWriteTable(con, "test_table", df, overwrite=FALSE, append=T, row.names=0)
df2 <- dbReadTable(con, "test_table")
Both fields come out exactly the same. It appears as if the timezones are completely discarded.
df2$dttm
[1] "2016-01-01 10:20:10 CET" "2016-01-01 14:20:10 CET"
df2$dttmtz
"2016-01-01 10:20:10 CET" "2016-01-01 14:20:10 CET"
QUESTIONS:
- What exactly goes on behind the scenes?
- How can I properly pass the POSIXct's timezone back and forth?