Setting up Postgres for Django on Debian Linux

Assumptions: Debian-based linux distro

1. Install postgres

sudo apt-get install postgresql

2. Create a password for the default postgres user

sudo passwd postgres

3. Login as PostGres user

su - postgres

4. Connect to postgres

psql

5. Start Database as root (automatically runs initdb)

service postgresql start

6. Create a role (the Django user in the DATABASES dictionary in settings.py is actually a role)

CREATE ROLE somebody WITH LOGIN PASSWORD 'secret123';

7. Create  a database

create database mydatabase

8. Now configure settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'somebody',
        'PASSWORD': 'secret123',
        'HOST': ''  #localhost
    }
}

Done!

Advertisements

Set Up New Relic for a Django project

1. Setup a New Relic account:

http://newrelic.com/signup

2. Install the newrelic app

pip install newrelic

2. Copy your license key

4. Create the new relic config file:

newrelic-admin generate-config LICENSE-KEY newrelic.ini

5. Edit the newrelic.ini file to give your application a name:

app_name = My Python Application

6. Update wsgi.py:

import newrelic.agent
import os

newrelic.agent.initialize('/home/username/path/to/myproject/newrelic.ini')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") 

from django.core.wsgi import get_wsgi_application 

application = get_wsgi_application()
application = newrelic.agent.wsgi_application()(application)

6. Verify it’s working:

newrelic-admin validate-config newrelic.ini

7. Start making request to the dev server and log into your New Relic account to see the data!

Setup Memcached for Django in a Development Environment

So I just started adding a caching layer for a large Django project and found that the initial setup was much less painful than I expected. Here are the steps so far:

1. install memcached system-wide.

sudo apt-get install memcached

2. install python bindings

pip install python-memcached

3. Add cache settings to settings.py (or local settings for specific configuration). Set the location to local ip address.

CACHES = {
    'default': {
        'BACKEND':'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION':'127.0.0.1:11211',
    }
}

4. Test that everything works.

from django.core.cache import cache
cache.get('foo')
cache.set('foo', 'bar')
cache.get('foo')
'bar'

From here the Django cache settings can be used to cache the whole site, per-view or template framgents: https://docs.djangoproject.com/en/dev/topics/cache/