Skip to content

Postgres returning bad dates, related to datestyle? #287

Closed

Description

@baderstine

Postgres returning dates/timestamps as very different from database values using R (3.6.3).

Within pgAdmin 4.x the source column value is:
2018-07-27 09:19:43 (column type is "timestamp without time zone")

con  = RPostgres::dbConnect(RPostgres::Postgres(), 
                            host ..., 
                            port ..., 
                            dbname ..., 
                            user ..., 
                            password ..., 
                            sslmode = 'require', 
                            timezone = NULL)

dbGetQuery(con, "select tzcolumn from tablename limit 1")

Using RPostgres 1.2.0 returns 2719-03-09 05:40:03

Using RPostgres 1.3.1 returns 2719-04-22 05:40:03

Also get really strange results from my db when running test-timezone.R, which really seems like a date formatting issue:

>   query <- "SELECT '2018-01-01 12:30:00'::TIMESTAMP AS a, '2018-01-01 12:30:00'::TIMESTAMPTZ AS b"
>   DBI::dbGetQuery(con, query)
                    a                   b
1 0120-12-21 06:10:00 0120-12-21 00:37:49

I noticed the same issue with date formatted columns.
source : output
"2010-04-10" : "1015-01-05",
"2009-12-30" : "3005-09-10"

In PgAdmin, I ran show datestyle and it returned "ISO, DMY".
On a different PostgreSQL database that I connect to using RPostgres (without SSL, not sure if that's relevant), show datestyle returns "ISO, MDY" and queries of this database return perfectly normal results for all of the above queries (timestamps, etc. are correct).

It seems that the first two characters of the returned date value are always the "day" of the original date, but cannot sort out how the other date characters are determined.

Activity

  1. baderstine commented on Jan 20, 2021

    @baderstine
    ContributorAuthor

    Another head-scratcher:
    DBI::dbGetQuery(con, "SELECT current_date;") returns

      current_date
    1   2020-09-08
    

    today is 2020-01-20, aka 20-01-2020 (DMY), aka 01-20-2020 (MDY)

  2. baderstine commented on Jan 20, 2021

    @baderstine
    ContributorAuthor

    @krlmlr you were right about datestyle it seems:

    Not sure why I didn't try this earlier.
    DBI::dbGetQuery(con, "show datestyle") returns

               DateStyle
    1 Redwood, SHOW_TIME
    

    If I explicitly set the datestyle then all subsequent results have properly formatted dates and times.
    DBI::dbExecute(con, "set datestyle to iso, mdy;")

  3. krlmlr commented on Jan 21, 2021

    @krlmlr
    Member

    Thanks. So the workaround seems obvious: issue a set datestyle query after connecting; we could also do this ourselves in dbConnect() .

    The question remains why libpq relies on this configuration, and how to retrieve values via libpq that work regardless of this configuration. It might be impossible, then the workaround is all that remains.

    Would you like to contribute a pull request?

  4. baderstine commented on Jan 21, 2021

    @baderstine
    ContributorAuthor

    sure, can do.

  5. added a commit that references this issue on Jan 22, 2021
  6. github-actions commented on Jan 23, 2022

    @github-actions
    Contributor

    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.

  7. locked and limited conversation to collaborators on Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions