In the actual application process, connecting Python to the PostgreSQL database is a headache, because you need to download the relevant version when connecting Python to the PostgreSQL database, the following article describes how to solve the problem.
1. Install the PostgreSQL database. You can use version 8.4.2 to download the database.
2. In ipvs, run the Windows Installation version directly;
3. after the installation is complete, the Database Service should run automatically, you can run the pgadminIII graphic interface management tool in the "Start"> "program"> "postgresql" folder to manage the psycopg package used for database installation and python connection, (in windows, I used the compiled binary version psycopg 2.0.13forpython2.6)
After the database connection module is installed, you can use the test method described above to connect to the database through django.
You can create a new apps) and write a model.
The system has an agreement on the app: if you use the Django database layer model), You must create a django app. The model must be stored in apps. Therefore, to start building our model, we must create a new app.
Create a model in the newly created models. py. Each model is equivalent to a single database table, and each attribute is also a field in this table. The attribute name is the field name. Its type is CharField, which is equivalent to the field type of the database, such as varchar ).
In the process of connecting Python to the PostgreSQL database, use the following command to verify the validity of the model:
- python manage.py validate
The validate command checks whether the syntax and logic of your model are correct. If everything is normal, you will see the 0 errors found message.
Run the following command to generate the create table statement:
- python manage.py syncdb
After executing this command, the database is automatically generated. It checks the database based on the app set in INSTALLED_APPS. If the table does not exist, it creates it. Note that syncdb cannot synchronize model modifications to the database.
Nothing happens because you have not added a new model or a new app. The above is an introduction to some of the operations related to connecting python to the PostgreSQL database.