Python Pyramid with PostgreSQL

I created an app on openshift. and then i added a postgreSQL cartridge there.

using ssh username@ip which redhat provided i logged into the server.

using command "export" to check out the postgreURL which looks like

postgre://username:password@ip:port

then store those information.

To make postgreSQL run with pyramid web application
(for ubuntu/mint/debian)
We should install ../bin/easy_install psycopg2
 or ../bin/pip install psycopg2
       (but there may some packages we need before we install this package)


Reference: http://pythonhosted.org/psycopg2/install.html#install-from-a-package
We should install this first:
  sudo apt-get install libpq-dev
(tool pg-config is inside of libpq-dev)
To compile psycopg2 we also need:
  sudo apt-get install python3-dev (please notice that this should be python3-dev)

Then
in the virtualenv,

../bin/easy_install psycopg2

After few minutes, it will be finished.


to check it out: then you go
../bin/python

>>>import psycopg
>>>

Nothing wrong there!
Congrats! You've installed psycopg2 on your computer :)

then
change things in development.ini which is in the folder of your app
then do this!
#sqlalchemy.url = sqlite:///%(here)s/alchemy_proj.sqlite
sqlalchemy.url = postgresql://[username]:[passwd]@[ip]:[port]


and go add something in model.py like this:

from sqlalchemy import (
    Column,
    Integer,
    Text,
    String, // add this line here
    )

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    )

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()


class MyModel(Base):
    __tablename__ = 'models'
    id = Column(Integer, primary_key=True)
    #name = Column(Text, unique=True)
    name = Column(String(255), unique=True)        // and add this line here
    value = Column(Integer)

    def __init__(self, name, value):
        self.name = name
        self.value = value
                                                          30,26         All


then go
../bin/initialize_alchemy_proj_db development.ini

this script will create database and sheets automatically according to development.ini

then go

../bin/pserve development.ini --reload

this means you go there and start a server . when the setting's changed there, the server will refresh the settings.

原文地址:https://www.cnblogs.com/spaceship9/p/3006880.html