Sometimes I make changes to my django models that causes syncdb to not run and I can't figure out why. Here is an example of an error I might get:
python manage.py syncdb . . ommitted for brevity . return self.get_query_set().get_or_create(**kwargs) File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 343, in get_or_create raise e psycopg2.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"
These messages are a bit opaque so one useful technique I found is to temporarily enable postgresql logging to a file:
sudo vim /etc/postgresql/8.3/main/postgresql.conf
Now add or uncomment the following:
log_destination = 'stderr' logging_collector = on log_directory = '/tmp' log_statement = all
Next you need to restart PostgreSQL (obviously you dont want to do this on a production system without taking the needed precautions!).
sudo /etc/init.d/postgresql-8.3 restart
Next I ran my syncdb command again and at the same time watched my PostgreSQL logs being generated in /tmp (I used the unix tail command to do that):
2010-03-29 14:29:22 SAST LOG: statement: INSERT INTO "auth_permission" ("name", "content_type_id", "codename") VALUES (E'Can add Clip', 64, E'add_clip') 2010-03-29 14:29:23 SAST ERROR: duplicate key value violates unique constraint "auth_permission_pkey" 2010-03-29 14:29:23 SAST STATEMENT: INSERT INTO "auth_permission" ("name", "content_type_id", "codename") VALUES (E'Can add Clip', 64, E'add_clip') 2010-03-29 14:29:23 SAST LOG: statement: ROLLBACK TO SAVEPOINT s1217227072_x1
Ok from that I could see that django was having trouble inserting entries into the auth_permissions table for my Clip model. The postgresql logging functions can prove indispensible when you just want to know what low level requests and responses are coming and going from your database.