开发者

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

开发者 https://www.devze.com 2023-02-05 13:43 出处:网络
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

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)?


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号