How to set timezone for Postgres psql?

前端 未结 4 2081
北海茫月
北海茫月 2020-12-29 04:07

How do I set timezone for psql to something other than my default (US/Central)? Here\'s what I\'ve tried so far:

$ psql
psql (9.1.4, server 9.0.4)
...

$ psq         


        
相关标签:
4条回答
  • 2020-12-29 04:45
    ALTER USER postgres SET timezone='Asia/Tokyo' ;
    
    0 讨论(0)
  • 2020-12-29 04:49

    The psql doc says:

    -v assignment
    --set=assignment
    --variable=assignment
    Perform a variable assignment, like the \set internal command. Note that 
    you must separate name and value, if any, by an equal sign on the command line....
    

    But with the timezone it does not seem to work, perhaps because because of this:

     These assignments are done during a very early stage of start-up, 
     so variables reserved for internal purposes might get overwritten later.
    

    So, it seems you must either use the SET command inside psql, or either set the PGTZ environment variable:

    PGTZ=PST8PDT psql -c 'show timezone'
    

    Of course, if you are OK with setting the timezone globally for the user (not just for this individual psql instance), you might set that variable in its .bashrc file (if in Linux)

    0 讨论(0)
  • 2020-12-29 04:50

    Note many third-party clients have own timezone settings overlapping any Postgres server and\or session settings.

    E.g. if you're using 'IntelliJ IDEA 2017.3' (or DataGrips), you should define timezone as:

    'DB source properties' -> 'Advanced' tab -> 'VM Options': -Duser.timezone=UTC+06:00

    otherwise you will see 'UTC' despite of whatever you have set anywhere else.

    0 讨论(0)
  • 2020-12-29 04:54
    psql (9.1.4)
    Type "help" for help.
    
    richardh=> show timezone;
     TimeZone 
    ----------
     GB
    (1 row)
    
    richardh=> set timezone='UTC';
    SET
    richardh=> show timezone;
     TimeZone 
    ----------
     UTC
    (1 row)
    
    richardh=> set timezone='US/Eastern';
    SET
    richardh=> show timezone;
      TimeZone  
    ------------
     US/Eastern
    (1 row)
    
    richardh=> set timezone='blah';
    ERROR:  invalid value for parameter "TimeZone": "blah"
    
    0 讨论(0)
提交回复
热议问题