nadine1988
nadine1988

Reputation: 167

Postgres error on insert - PG::Error: ERROR: invalid byte sequence for encoding "UTF8": 0xe073

I get the following error when inserting data from mysql into postgres.

PG::Error: ERROR:  invalid byte sequence for encoding "UTF8": 0xe073
: INSERT INTO "places" ("accent_city", "city", "country", "created_at", "latitude", "longitude", "region", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"

I want to insert this file worldcitiespop.txt in my PG database as a rake task

namespace :places_db do
    desc "save cities in database"
    task save_places: :environment do
        File.open("lib/city_name/worldcitiespop.txt", "r").each_line do |row|
            row = row.force_encoding('ISO-8859-1').split(',')
            Place.create(country: row[0], city: row[1], accent_city: row[2], region: row[3], latitude: row[5], longitude: row[6])
        end 
    end
end

Upvotes: 0

Views: 1181

Answers (1)

Daniel Vérité
Daniel Vérité

Reputation: 61726

Since the file is encoded with ISO-8859-1, the simplest way is to let the database do the conversion by issuing the following SQL query just before importing:

SET client_encoding=latin1;

To go back to the previous encoding after the import:

SET client_encoding=default;

This setting affects only the current session.

Upvotes: 1

Related Questions