This was originally posted on my tumblelog about a month ago and is reproduced here to get the ball rolling.
This is mainly for my notes, so I’ll apologise now if this doesn’t work for you.
The first thing to do is back up our data into an easy to use (mostly) database agnostic (mostly) format. Django provides the dumpdata and loaddata commands to do this. To dump data for the analyse application to a file called dumpdata-desktop.json you do the following (from your project directory):
python manage.py dumpdata analyse > dumpddata-desktop.json
To dump everything just use dumpdata with no arguments and redirect the output to a file. Make sure you have enough memory to do this as earlier versions of Django don’t stream dumpdata output. I keep a copy of htop running whilst I’m doing this. If it seems really inefficient that’s because it is. The analyse app I’m dumping uses a 45mb sqlite3 database, but dies on anything less than a few gig of memory for dumping. If you’re on a resource constrained system, you can always move the sqlite database over to a django installation with more memory. Another option is to use a large swapfile/partition, but that’s going to slow things down substantially. If you’re really stuck, you can dumpdata individual applications and models. Once that’s done, you should have a serialized file containing all your django goodness ready for import into postgresql, mysql or anything else django can work with.
Now we need to set up postgreSQL. The first thing to do is to create a django user. The easiest way of doing this is to sudo -H -s to root and issue the following:
sudo -u postgres createuser -P analys
Replace analys with the username you want to use. Note that I’m using analys, not analyse as this is a reserved keyword for postgreSQL. Now we need to create a database for the user to use. In my case I need two databases (one for two similar but slightly different applications) that will use the same user. It may be better to use separate users for separate databases if some segregation of data is required, but I don’t need it for my two apps.
sudo -u postgres psql template1
CREATE DATABASE analys_desktop OWNER analys ENCODING ‘UTF8′;
CREATE DATABASE analys_server OWNER analys ENCODING ‘UTF8′;
Once the users and databases are created it’s time to give django access. To do this we’ll need to change the pg_hba.conf file, normally contained within something like /etc/postgresql/8.3/main/pg_hba.conf on Debian-based systems.
Go down to the section that specifies the users and how they connect and add the following:
local analys_desktop analys md5
local analys_server analys md5
Make sure you replace the database and user settings with your own. Restart postgresql. Assuming all goes well, we’re ready to set up Django.
Putting it all together
Now all we need to do is configure Django to use the new database and load the database back in. Go into settings.py and change the following settings:
DATABASE_ENGINE = ‘postgresql_psycopg2′ # ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ’sqlite3′ or ‘ado_mssql’.
DATABASE_NAME = ‘analys_desktop’ # Or path to database file if using sqlite3.
DATABASE_USER = ‘analys’ # Not used with sqlite3.
DATABASE_PASSWORD = ‘password’ # Not used with sqlite3.
DATABASE_HOST = ” # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ” # Set to empty string for default. Not used with sqlite3.
Changet the DATABASE_PASSWORD value to the actual database password. Next we need to run python manage.py syncdb. Don’t create a superuser if you did a full dump. Finally we use the loaddata command to restore our data.
python manage.py loaddata dumpdata-desktop.json
There, all done!