Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding error with Rails, PostgreSQL and non-standard characters

I'm getting the following error:

ActiveRecord::StatementInvalid: PG::Error: ERROR: invalid byte sequence for encoding "UTF8": 0xf66e6bf6 : INSERT INTO "response_sets" ("city") VALUES ('Jönköping') RETURNING "id"

Database is PostgreSQL 9.0.6 on a Heroku app.

Not sure how to get around that error when there are odd characters.

like image 892
Shpigford Avatar asked Oct 04 '12 13:10

Shpigford


1 Answers

Your database isn't set to the same encoding scheme as the string you're trying to insert. I would imagine Postgres on Heroku is set up to use UTF-8 by default and your input might be one of the latin variations if I had to guess. You can either set your database to accept the encoding scheme you're supplying, e.g.:

SET CLIENT_ENCODING 'ISO-8859-2'

Or you can transcode your input to UTF-8 (this is probably better)

"my string".encode('UTF-8')
like image 183
Peter Duijnstee Avatar answered Sep 22 '22 17:09

Peter Duijnstee