PostgreSQL - rezmehp/tutorial GitHub Wiki

PostgreSQL Database Download

Define Postgres password

  • \password postgres
  • \l see Database info

from pdAdmin create new Server

  • from propertis set security setting

install some postgres pakage

  • pip install psycopg2

change and set postgres in setting

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'datbasename', 'USER': 'a_user', 'PASSWORD': 'a_password', 'HOST': 'localhost', 'PORT': '5432', } }

use Migrate to create stufe in database

  • py manage.py migrate

Model filds info

This example model defines a Person, which has a first_name and last_name:

from django.db import models

class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)

first_name and last_name are fields of the model. Each field is specified as a class attribute, and each attribute maps to a database column.

The above Person model would create a database table like this:

CREATE TABLE myapp_person ( "id" serial NOT NULL PRIMARY KEY, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL );

Some technical notes:

The name of the table, myapp_person, is automatically derived from some model metadata but can be overridden. See Table names for more details.
An id field is added automatically, but this behavior can be overridden. See Automatic primary key fields.
The CREATE TABLE SQL in this example is formatted using PostgreSQL syntax, but it’s worth noting Django uses SQL tailored to the database backend specified in your settings file.