What should a Django user know when switching from MySQL to PostgreSQL?

Most of my experience with Django so far has been with MySQL and mysqldb. For the new application I'm writing, I dip my finger in the water of PostgreSQL, now that I have seen the light .

While writing a script data import, I came across a problem with autocommit default behavior. I would suggest that other "sweeps" may occur. What else should I be looking for?

+6
django mysql postgresql psycopg2
source share
1 answer

There is inconsistency between Django startup and PostgreSQL default commit mode.

Out of the box, Django uses PostgreSQL's default β€œread commit” mode, which combines all operations into a single transaction, which ends when the db cursor goes out of scope. A problem occurs when an error occurs during this series of operations. Postgres expects you to roll back before continuing, and if you do not, psycopg2 throws an InternalError the next time it connects. If you rely on Django's autorun (the default), you're probably not going to roll back properly.

Fortunately, psycopg2 supports a different mode of operation called "autocommit" in which it does not set these transactions up. For those who come from MySQL (or try to support both), this brings mind to the world. In 1.1, they added support to expose it. Add the following settings to your settings (you need to change the syntax for 1.2 if you are in the trunk)

DATABASE_OPTIONS = { "autocommit": True, } 

The discussion for Django ticket # 3460 contains details.


+11
source share

All Articles