Friday, October 2, 2015

PostrgreSQL with Django

Installation:
Follow this link for PostgreSQL installation instructions:
http://www.techtrekking.com/2015/09/installing-python-select-latest-version.html

Getting started 
1. Open pgAdmin3.exe from C:\Program Files\PostgreSQL\9.4\bin or whichever folder you have installed it.
2. Right click on PostgreSQL 9.4 (Localhost:5432) and click Connect
3. Now go to models.py and add one model. Sample Model.py as belowfrom 

django.db import models
class Searchterms(models.Model):    
name = models.CharField(max_length=150)
def __unicode__(self):  #For Python 2, use __str__ on Python 3        
return self.name

4. Update settings.py as below:
DATABASES = {    
'default': {        
'ENGINE': 'django.db.backends.postgresql_psycopg2',        
'NAME': 'postgres',        
'USER': 'postgres',        
'PASSWORD': 'password_that_you_set_while_installing_PostgreSQL',
'HOST': 'localhost',       
'PORT': '5432',    }
}
5. Now go to command prompt and run below commands
C:\Python27\literating>python manage.py makemigrations
C:\Python27\literating>python manage.py migrate

Operations to perform:  Synchronize unmigrated apps: staticfiles, messages  
Apply all migrations: admin, contenttypes, search, auth, sessionsSynchronizing apps without migrations:  
Creating tables...    
Running deferred SQL...  
Installing custom SQL...
Running migrations:  
Rendering model states... DONE  
Applying contenttypes.0001_initial... OK  
Applying auth.0001_initial... OK  
Applying admin.0001_initial... OK  
Applying contenttypes.0002_remove_content_type_name... OK  
Applying auth.0002_alter_permission_name_max_length... OK  
Applying auth.0003_alter_user_email_max_length... OK  
Applying auth.0004_alter_user_username_opts... OK  
Applying auth.0005_alter_user_last_login_null... OK  
Applying auth.0006_require_contenttypes_0002... OK  
Applying search.0001_initial... OK  
Applying search.0002_auto_20150911_2246... OK  
Applying sessions.0001_initial... OK

6. If you run migrate command without making any changes you would see below result on command prompt:
C:\Python27\literating>python manage.py 
migrateOperations to perform:  
Synchronize unmigrated apps: staticfiles, messages  
Apply all migrations: admin, contenttypes, search, auth, sessions
Synchronizing apps without migrations:  
Creating tables...    
Running deferred SQL...  
Installing custom SQL...
Running migrations:  
No migrations to apply.

7. Further details about how to create database can be found here: http://www.postgresqltutorial.com/postgresql-create-database/ 


Accessing PostgreSQL data base through command line.
Open windows command prompt. If below commands gives you an error try opening it with administrative rights.
C:\Python27\literating>psql --username=postgres
Password for user postgres:
psql (9.4.4)
WARNING: Console code page (437) differs from Windows code page (1252)         8-bit characters might not work correctly. See psql reference         page "Notes for Windows users" for details.Type "help" for help.

postgres=# \list
List of databases   
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 mydb      | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres  +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres+

           |          |          |                            |                            | postgres=CTc/postgres

(4 rows)

postgres=# \connect mydb

WARNING: Console code page (437) differs from Windows code page (1252)         8-bit characters might not work correctly. See psql reference         page "Notes for Windows users" for details.You are now connected to database "mydb" as user "postgres".
postgres=# \dt                   
List of relations Schema |            Name            | Type  |  Owner
--------+----------------------------+-------+---------- 
public | auth_group                 | table | postgres 
public | auth_group_permissions     | table | postgres 
public | auth_permission            | table | postgres 
public | auth_user                  | table | postgres 
public | auth_user_groups           | table | postgres 
public | auth_user_user_permissions | table | postgres 
public | django_admin_log           | table | postgres 
public | django_content_type        | table | postgres 
public | django_migrations          | table | postgres 
public | django_session             | table | postgres 
public | search_searchterms         | table | postgres
(11 rows) 

Running Queries on command prompt:

mydb=# SELECT datname FROM search_searchterms;

Getting list of databases 
mydb=# SELECT datname FROM pg_database
mydb-# WHERE datistemplate = false; 
datname
---------- 
postgres 
mydb
(2 rows)

Alternatively type \? To get full list of commands.Hit Ctr+c to come out of PostreSQL database.

No comments:

Post a Comment