开发者

PGError: ERROR: unrecognized time zone name: "UTC"

开发者 https://www.devze.com 2023-02-12 06:02 出处:网络
Is there anything wrong with this (User model, line 95): scope :by_recent, where(\'created_at BETWEEN ? AND ?\', 1.month.ago, Time.now).order(\'created_at desc\').

Is there anything wrong with this (User model, line 95):

scope :by_recent, where('created_at BETWEEN ? AND ?', 1.month.ago, Time.now).order('created_at desc').

Whenever I start rails s or rails c, I get:

 /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': PGError: ERROR:  unrecognized time zone name: "UTC" (ActiveRecord::StatementInvalid)

Commenting that line in my model, makes the error go away. Strange. I am using postgres 9 by the way. Installed via Homebrew (brew install posgtres).

This link, suggests the following below. But I don't understand what the person is suggesting as a solution...

MikZ: i've figured this out

rails are setting accessors with automatic timezone conversions when you override acessor (with table colum) you will get to serious problems

when leaving rails default (or setting timezone) you will get nice results but if you override accessors you will get to serious problems with that

so: don't ever override "time" attibutes' accessors

Full error:

/Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': PGError: ERROR:  unrecognized time zone name: "UTC" (ActiveRecord::StatementInvalid)
: SET time zone 'UTC'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:496:in `execute'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:961:in `configure_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:939:in `connect'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:228:in `initialize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `new'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `postgresql_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in `new_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `checkout_new_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `block (2 levels) in checkout'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `loop'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `block in checkout'
    from /Users/Chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `checkout'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activereco开发者_高级运维rd-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:93:in `connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1330:in `replace_bind_variables'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1317:in `sanitize_sql_array'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1224:in `sanitize_sql_for_conditions'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:206:in `build_where'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:77:in `where'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:441:in `where'
    from /Users/Chris/Sites/site_name/app/models/user.rb:95:in `<class:User>'
    from /Users/Chris/Sites/site_name/app/models/user.rb:1:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `block in load_file'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:453:in `load_file'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:491:in `load_missing_constant'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:183:in `block in const_missing'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `const_missing'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:124:in `block in constantize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `constantize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:528:in `block in <class:Reference>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `yield'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `default'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `get'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/mapping.rb:76:in `to'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:164:in `block in devise_for'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:160:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:160:in `devise_for'
    from /Users/Chris/Sites/site_name/config/routes.rb:75:in `block in <top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `draw'
    from /Users/Chris/Sites/site_name/config/routes.rb:1:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `block in load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `block in reload_routes!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `reload_routes!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:120:in `block in routes_reloader'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `call'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:51:in `block (2 levels) in <module:Finisher>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `call'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `block in <module:Finisher>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    from /Users/Chris/Sites/site_name/config/environment.rb:5:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:103:in `require_environment!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:22:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

EDIT:

Actually, the problem is related to this issue. I've also moved from macports to homebrew. Do I need to remove /opt/local before installing postgres with homebrew?

Second, I can do rake db:drop:all and rake db:create, but not rake db:migrate. I get:

rake aborted!
PGError: ERROR:  unrecognized time zone name: "UTC"
: SET time zone 'UTC'


Yeap, it may have seemed that there is something seriously broken with the database. I remembered installing PostgreSql via homebrew, but possibly not stopping the macport installation of the postgres server, that was still running in the background.

Through the assistance of another developer (RhodiumToad), I was able to fix this by doing the following steps:

  1. stop the server
  2. re-run initdb
  3. start the server again

Earlier, I removed /usr/local/var/postgres, via rm -rf /usr/local/var/postgres. So this is what I needed to do:

ps axw

find the postgres server

117   ??  S      0:00.23 /usr/local/Cellar/postgresql/9.0.3/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.

kill it, which should also kills any other postgres process' running in the bg

kill 117

The above killed the process and also stopped the server. Now I had to initialize the database and everything seems to work correctly again:

initdb /usr/local/var/postgres

Hope this helps someone one who comes across the same issue.


My problem was that I had multiple instances of PostgreSQL server running. Killing every process worked and restarting only the one I wanted worked. Not sure why this was causing PGError: ERROR: unrecognized time zone name: "UTC" : SET time zone 'UTC'

 ps axw | grep postgres

Then kill as Christian said above.

NOTE: I didn't need to run initdb again.


not 100% sure but the way you define the scope makes a string out of the Time parameters you pass in, which may be causing some issues. try using symbols instead and see if that works:

scope { :conditions => {:created_at => 1.month.ago..Time.now}, :order => 'model.created_at DESC' }


Well, I'm guessing that something in that line in your model is making it try to fix down the time zone in use when talking to the database. I guess it makes a kind of sense...

Anyway, assuming that the string SET time zone 'UTC' is the command being sent to postgresql on connection (which seems to be the case, looking at that backtrace) then I would expect it to work. Certainly it works on my own (Linux) installation of postgresql. Can you verify that it fails if you connect to the database with psql and issue the command manually?

It does seem to indicate a broken postgresql installation. Cleaning things away and reinstalling would be a reasonable step to try. My own installation of postgresql is using the timezone data from the operating system, i.e. looking in /usr/share/zoneinfo. It's possible that your installation is trying to pick up the data from some location that was present during build but has been removed since as you transitioned over. I believe it should be that it can't find a location with the data at configure time, it will install its own bundled copy.


This error happens because Postgres was upgraded but not restarted. Old version running was looking for the un-existing timezone file.

Just restarted as said here: https://stackoverflow.com/a/20566779/1682263

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号