简体   繁体   中英

Heroku Postgresql SET datestyle To european

I am addressing an issue where I cannot copy data to postgresql in Rails because the date format is not in matching the default postgresql ISO MDY.

On my local machine I just changed the datestyle to European so that my DMY data matches correctly.

As usual, its not as easy to do this on the Heroku production server. Using the steps below I try to change the datestyle, which works, but it doesn't persist (as shown by exiting, going back in and asking postgres to show me the datestyle).

Any ideas how I can persist this. All research has show not to do this and to change my code to convert the data to MDY but I am pulling millions of rows from a legacy system that defaults as DMY and using a postgresql copy command to do this the fastest way possible. Seems easier to just tell Postgresql to change datestyle than fix everything else :/

$ heroku pg:psql    
app-name::CRIMSON=> SET datestyle TO 'european';
SET
app-name::CRIMSON=> SHOW datestyle;
 DateStyle 
-----------
 ISO, DMY
(1 row)

app-name::CRIMSON-> \q
$ heroku pg:psql
---> Connecting to HEROKU_POSTGRESQL_CRIMSON_URL (DATABASE_URL)
psql (9.4.1, server 9.3.6)
SSL connection (protocol: TLSv1.2, cipher: jibberish, bits: 256, compression: off)
Type "help" for help.

app-name::CRIMSON=> SHOW datestyle;
 DateStyle 
-----------
 ISO, MDY
(1 row)

Any ideas how I can persist this.

datestyle is a server setting (in postgresql.conf) that client sessions can override (with set datestyle ... ) for a single transaction or for a single session. There are two ways to persist a change beyond a single session.

  • Change the setting in postgresql.conf and restart the server. (Won't work for you on Heroku. You surely don't have privileges to edit postgresql.conf, right?)
  • ALTER DATABASE your_database_name SET datestyle TO 'ISO, european'; (Should work for you on Heroku. I'd set this up as a stand-alone migration.)

FWIW, the default isn't ISO MDY , it's ISO, MDY . (Two values, separated by a comma.) The first is the "output format specification"; the second is the "input/output specification for year/month/day ordering". (See Client Connection Defaults )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM