. . .use PostgreSQL with Flask or Django.

Install Postgres

First, download and install Postgres for your OS here. python

Sidenote for MacOSX users: I have found Postgres.app to be the simplest option1. Just download and run the app. sql

# check that you successfully installed postgres $ which psql
/Applications/Postgres.app/Contents/MacOS/bin/psql

Create a new user and a database

# Create a new user $ createuser -U postgres yourusername -P
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n # Create a new database $ createdb -U yourusername -E utf8 -O yourusername yournewnewdb -T template0

Set up Postgres with Flask or Django

# install psycopg2 $ pip install -U psycopg2 # If using SQLAlchemy $ pip install Flask-SQLAlchemy

Flask

# Flask config.py SQLALCHEMY_DATABASE_URI = "postgresql://yourusername:yourpassword@localhost/yournewdb"
# Flask app.py from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_pyfile(config.py) db = SQLAlchemy(app)

Django

# Django settings.py 
DATABASES = { 'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 
'NAME': 'yournewdb', 
'USER': 'yourusername', 
'PASSWORD': 'yourpassword', 
'HOST': 'localhost', 
'PORT': '5432', }



That's it! Go ahead and write your models.
相關文章
相關標籤/搜索