在openshift上使用django+postgresql

openshift上用的是django 1.7,数据库选择的是postgresql 9.2

本地开发用的是sqlite3数据库,发布到openshift上后是没有数据的(本地的sqlite3数据库里的数据并没有添加到openshift的数据库里,因为服务端的是postgresql数据库)。那么怎么在openshift上操作postgresql数据库呢?Let's go!

一、连接openshift上的应用

$ rhc ssh myapp

二、激活python虚拟环境

source $OPENSHIFT_HOMEDIR/python/virtenv/venv/bin/activate

三、开启django shell

python "$OPENSHIFT_REPO_DIR"wsgi/manage.py shell

进入界面:

Python 3.3.2 (default, Mar 20 2014, 20:25:51) 

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)

>>>

在shell里就可以操作数据库了

>>> from django.db import connection

>>> cur=connection.cursor

>>> cur

<bound method DatabaseWrapper.cursor of <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object at 0x7f1fe2a175d0>>

给postgresql的数据表里添加一条记录:

>>> from myblog.models import Todolist

>>> q=Todolist(title='hello world')

>>> q

<Todolist: hello world>

>>> q.save()

>>> q

<Todolist: hello world>

>>> Todolist.objects.all()

[<Todolist: hello world>]

 

--End--

你可能感兴趣的:(PostgreSQL)