psj
psj

Reputation: 164

How can I use Django with a legacy Postgresql database whose data is encoded in LATIN1?

Django seems to want its database encoded in UTF-8, but our legacy database is encoded in LATIN1, which legacy systems require. It is possible/feasible to hook Django's db-access stuff to translate between UTF-8 and LATIN1 when reading/writing from/to the database? Is there a better solution (that doesn't require converting the db)?

Upvotes: 2

Views: 590

Answers (2)

Magnus Hagander
Magnus Hagander

Reputation: 25088

PostgreSQL will translate it for you if you set client_encoding to UTF8, as long as your database is in LATIN1 (and not in SQLASCII). You can either have django send a SET client_encoding='UTF8' command, or you can change the default in postgresql.conf.

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186562

I suggest dumping the sql file and using iconv to convert everything to UTF-8.

You can use something similar to this which I used for cyrillic ( Russian ) Latin1 to UTF-8:

iconv -f utf-8 -t latin1 < in.sql | iconv -f cp1251 -t utf-8 > out.sql

Upvotes: 0

Related Questions