Changing Django database backend from MySql to PostgreSQL

微笑、不失礼 提交于 2019-12-03 03:48:28

I got tired of seeing this error using South and yes, switching to PostgreSQL has banished it!

The mysql2postgres app, written in Ruby, suggested in comments above didn't work for me (it would run, output some details to screen but not copy any rows of data, for me). Not sure why. But gladly there's a Python rewrite of it that worked flawlessly (for me, eventually):
http://pypi.python.org/pypi/py-mysql2pgsql

The only gotcha I found was:

Initially I thought it would be safest to set up the tables in the PostgreSQL db via a syncdb and then migrate the data only. I tried this, but the tables are migrated across in alphabetical order and this violates the foreign key constraints for some tables (rows relate to rows in tables not yet imported).

I next tried a structure+data migration. This migrated fine, but I encountered some problems in Django afterwards, especially the admin site. It seemed like the migration script had created some different table constraints from what Django would have.

I solved this by hacking the mysql2pgsql script to respect the order of tables given in the yaml config only_tables property... and then doing a syncdb + data-only migration. By trial and error I shuffled around the ordering of the tables for my migration until they all imported successfully.

UPDATE:
My pull request for the hack described above got accepted so you can do this now from the main version:
https://github.com/philipsoutham/py-mysql2pgsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!