How to configure Postgres to accept date in PT-BR format?

2

Situation

Let's say I have a form on which the user fills a date field, DD-MM-YYYY.

Question

How can I make postgres accept this format normally?

  • No need to to_timestamp('14/06/2016', 'DD-MM-YYYY')

Addendum

  • SELECT be YYYY-MM-DD .
  • The INSERT accept YYYY-MM-DD or DD-MM-YYYY .
asked by anonymous 14.06.2016 / 20:32

1 answer

5

Searching a little bit found this answer .

The steps are simple:

  • Check your current datestyle "ISO, DMY" or "IOS, MDY".
  • Now Depending on what you want in postgres DMY or DMY

    ALTER DATABASE "my_database_name" SET datestyle TO "ISO, DMY";

    or

    ALTER DATABASE "my_database_name" SET datestyle TO "ISO, MDY";

  • After this you will have to restart the server

    sudo service postgresql restart

    Or

    sudo /etc/init.d/postgresql restart

  • Test the configuration

    Query: SELECT '20 / 12/2016 ':: date Output: "2016-12-20"

    Or

    Query: SELECT '12 / 19/2016 ':: date Output: "2016-12-19"

  • In this case we use, ALTER DATABASE "my_database_name" SET datestyle TO "ISO, DMY"; .

        
    14.06.2016 / 20:41