Archive

Posts Tagged ‘development’

[django] using PostgreSQL for local development

August 4, 2014 Leave a comment

PostgreSQL is highly recommended for Django. In the book “Two Scoops of Django” it is written that you should use the same DBMS that you will use in production. Many people use SQLite3 for local development and PostgreSQL in production but that will lead to surprises. So, if you plan to use PostgreSQL in production, then use PostgreSQL on your localhost too.

Question
How to set up and configure PostgreSQL on localhost? How to make it work with Django?

Answer
I found the answers to these questions here: How To Install and Configure Django with Postgres, Nginx, and Gunicorn.

PostgreSQL for local development
Long version:

# switch to user "postgres"
$ sudo su - postgres
[sudo] password for jabba:
# notice "postgres" in the prompt
postgres@jabba-uplink:~$ createdb mydb

# create a database user if you don't have one yet
postgres@jabba-uplink:~$ createuser myuser -P
Enter password for new role: 
Enter it again:

# grant access
postgres@jabba-uplink:~$ psql
psql (9.3.5)
Type "help" for help.

postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
GRANT
postgres=#

Short version (if you already have a database user):

$ sudo su - postgres
postgres$ createdb mydb
postgres$ psql
psql (9.3.5)
Type "help" for help.

postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
GRANT
postgres=#
Categories: django Tags: , , ,

Heroku: development and production settings

November 2, 2013 Leave a comment

Problem
You have a project that you develop on your local machine and you deploy it on Heroku for instance. The two environments require different settings. For example, you test your app. with SQLite but in production you use PostgreSQL. How can the application configure itself to its environment?

Solution
I show you how to do it with Flask.

In your project folder:

$ heroku config:set HEROKU=1

It will create an environment variable at Heroku. These environment variables are persistent – they will remain in place across deploys and app restarts – so unless you need to change values, you only need to set them once.

Then create a config.py file in your project folder:

import os

class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite://:memory:'

class ProductionConfig(Config):
    """
    Heroku
    """
    REDIS_URI = os.environ.get('REDISTOGO_URL')

class DevelopmentConfig(Config):
    """
    localhost
    """
    DEBUG = True
    REDIS_URI = 'redis://localhost:6379'

class TestingConfig(Config):
    TESTING = True

Of course, you will have to customize it with your own settings.

Then, in your main file:

...
app = Flask(__name__)

if 'HEROKU' in os.environ:
    # production on Heroku
    app.config.from_object('config.ProductionConfig')
else:
    # development on localhost
    app.config.from_object('config.DevelopmentConfig')
...

Now, if you want to access the configuration from different files of the project, use this:

from flask import current_app as app
...
app.config['MY_SETTINGS']

Redis
Let’s see how to use Redis for instance. Apply the same idea with other databases too. Opening and closing can go in the before_request and teardown_request functions:

from flask import g
import redis

@app.before_request
def before_request():
    g.redis = redis.from_url(app.config['REDIS_URI'])

@app.teardown_request
def teardown_request(exception):
    pass    # g.redis doesn't need to be closed explicitly

If you need to access redis from other files, just import g and use g.redis .

Links