问题
I am totally new to pyramid.I am trying to develop an application that will use both pyramid angd postgresql but i am totally confused about how to configure the initializedb.py file in the sripts directory especially initializing the database.
I'm using PostgreSQL 9.1.
回答1:
If I understand your question, you just need to find out where to configure the connection to DB. I assume you created a sqlAlchemy based project. In the root of the pyramid app you can find the ini files. By default you should have development.ini and production.ini files. The former is used for development and the latter for production environment, so they can (and should) differ for some configurations.
In both files you should find a line like this:
sqlalchemy.url = sqlite:///%(here)s/dbname.sqlite
You just need to change that line with something like this:
sqlalchemy.url = postgresql://DBUser:DBPassword@DBHostNameOrIP:5432/DBName
(sustitute parameters as needed)
Whe you use initializedb.py directly, but you should use the initialize_YourProject_db script (in the bin folder, one dir back from the pyramid root), like this:
initialize_YourProject_db development.ini
where "YourProject" is the name of your project as defined when you created it and development.ini is the ini file you wish to use.
If have not done it yet, you may need to add 'db-psycopg2' to your required modules in setup.py and execute
pip install -e .
again (the final dot is not a typo). Please remember that psycopg2 (the driver for postgresql) has dependencies (on ubuntu 14_04 you need a 'apt-get install libpq-dev python-dev', it may differ in other distros/SOs).
If enaything works fine, the script will create all tables referenced, according to your models, and insert all items defined in initializedb.py and added to the DBSession
来源:https://stackoverflow.com/questions/24695220/pyramid-postgresql-connection