How can I easily convert a Django app from mySQL to PostgreSQL?

后端 未结 7 1980
一个人的身影
一个人的身影 2021-01-31 12:04

Has anyone done this? Is it an easy process? We\'re thinking of switching over for transactions and because mysql seems to be \"crapping out\" lately.

7条回答
  •  轮回少年
    2021-01-31 12:14

    Converting MySQL database to Postgres database with Django

    First backup your data of the old Mysql database in json fixtures:

    $ python manage.py dumpdata contenttypes --indent=4 --natural-foreign > contenttype.json
    $ python manage.py dumpdata --exclude contenttypes --indent=4 --natural-foreign > everything_else.json
    

    Then switch your settings.DATABASES to postgres settings.

    Create the tables in Postgresql:

    $ python manage.py migrate
    

    Now delete all the content that is automatically made in the migrate (django contenttypes, usergroups etc):

    $ python manage.py sqlflush | ./manage.py dbshell
    

    And now you can safely import everything, and keep your pk's the same!

    $ python manage.py loaddata contenttype.json
    $ python manage.py loaddata everything_else.json
    

    Tested with Django==1.8

提交回复
热议问题