11
votes

I am upgrading Rails 4.1 app to use Ruby 2.2. During upgrade it turned out that the app used old version of pg gem (0.12) and gem was not installing using Ruby 2.2. So I tried to use newer version of pg gem (0.17.1). Now I sometimes get following error when a new user is created in the system:

PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8"

Are there any known issues with Ruby 2.2 and Postgres?

Looks like application is configured to use unicode: config.encoding = "utf-8" is set in application.rb and encoding: unicode is in database.yml

SQL statement:

INSERT INTO "users" (
  "confirmation_sent_at", 
  "confirmation_token", 
  "created_at", 
  "email", 
  "encrypted_password", 
  "first_name", 
  "last_name", 
  "updated_at") 
  VALUES ($1, $2, $3, $4, $5, $6, $7, $8) 
  RETURNING "id"
2
I tried using pg 0.17.1 with ruby 2.1.5 and there are no encoding errorsAndrew Bezzub
@RustamA.Gasanov: this is not a duplicate, error does not originate from user input and did not happen before upgrade to ruby 2.2Andrew Bezzub

2 Answers

6
votes

Looks like this is a known issue with "pg" gem: https://bitbucket.org/ged/ruby-pg/issue/197/ruby-220-byte-encoding-issue

It should be fixed in 0.18 pre-release

6
votes

There is definitely some kind of bug in Ruby 2.2.0 re UTF-8 encoding with Postgres. I upgraded a previously bug free Rails 4.1.5 app from Ruby 2.1.5 to Ruby 2.2.0 and started getting these errors "PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding UTF8" on user input email fields in both development and in a staging environment on Heroku. Downgrading to Ruby 2.1.5 with no other changes fixed the issue completely.

update:

Beat to the punch on this update but the bug does not lie in Ruby but rather the pg gem: https://github.com/ged/ruby-pg/pull/5