Old Rails and PostgreSQL 12 Problem
When your Ruby on Rails is an old version and PostgreSQL is new, you will get the panic
error since PostgreSQL 12 doesn’t allow the panic value.
For example, we’re using Rails v4.2.1 and try to rake db:create
PG::InvalidParameterValue: ERROR: invalid value for parameter "client_min_messages": "panic"
HINT: Available values: debug5, debug4, debug3, debug2, debug1, log, notice, warning, error.
: SET client_min_messages TO 'panic'
The panic
will be at line 313
https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L313
It’s fixed from panic
to warning
since Rails v4.2.5 and above.
https://github.com/rails/rails/blob/v4.2.5/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L673
So, our purpose is to fix warning level for client_min_messages avoid error if Rails < 4.2.5 and PostgreSQL >= 12.
And these are the solutions I found.
Solution A
Update Rails gem to 4.2.5, fix Gemfile gem 'rails'
with this
gem 'rails', '4.2.5'
However, if we tried to update the Rails gem, we also need to update other dependency gems when installing the gem. 😛
Solution B
Find the location gem and fix it directly
$ locate 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb'/Users/pagorn/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb
Use your text editor to edit it. Go to line 313, you should found this code
def set_standard_conforming_strings
old, self.client_min_messages = client_min_messages, 'panic'
Change panic
to warning
It should look like this.
def set_standard_conforming_strings
old, self.client_min_messages = client_min_messages, 'warning'
save it and try rake db:create
I do not recommend this way because when we deploy/clone our project to the new environment, we need to fix it for the first time. Your colleagues will not happy. 😒
Solution C
- Fork Rails repository to your Github
- Clone it from your fork with specific tag version such as 4.2.1
git clone -b 'v4.2.1' https://github.com/rails/rails.git
cd rails
- Checkout a new branch from tag
git checkout -b fix/panic-error-postgresql-12
- Fix the
panic
towarning
def set_standard_conforming_strings
old, self.client_min_messages = client_min_messages, 'warning'
- commit code and push the new change to remote
git add activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
git commit -m 'Fix panic to warning for postgresql 12'
git push -u fix/panic-error-postgresql-12
- create tag and push a new tag (optional)
git tag 4.2.1.1
git push origin 4.2.1.1
After that, fix Gemfile to point it to branch or tag
- point to branch
gem 'rails', git: 'https://github.com/nrogap/rails.git', branch: 'fix/panic-error-postgresql-12'
- or point to tag (optional)
gem 'rails', git: 'https://github.com/nrogap/rails.git', tag: 'v4.2.1.1'
I already made tag v4.2.1.1 on Github for myself 😊
Solution D
Clone the whole rails gem
git clone -b 'v4.2.1' https://github.com/rails/rails.git
Paste it in the vendor folder
Fix the panic
similar Solution B, then fix Gemfile
gem 'rails', path: 'vendor/gems/rails-4.2.1.1'
The size of your project folder will be increase by ~22.5MB 🙂
Conclusion
In my opinion, I prefer Solution C and D. It better when you set up and run a project without any additional config.
I also tried the monkey patch solution but doesn’t work with the rake
command.
That’s it! Enjoys 🎉
References
- https://stackoverflow.com/questions/58763542/pginvalidparametervalue-error-invalid-value-for-parameter-client-min-messag
- https://gist.github.com/x-yuri/d76b487c49698cd80edd511ed58bfb81
- https://stackoverflow.com/questions/791959/download-a-specific-tag-with-git
- https://devconnected.com/how-to-checkout-git-tags/