Install psycopg2 (PostgreSQL adapter for Python) on OSX

I was playing with Django with Postgres backend, and I had little difficulty installing “psycopg2” the Python DB adapter for Postgres on my Mac OSX.

I’ve installed Postgres using Postgres.app for OSX which is straight forward and standard Mac app.

But when I tried installing “psycopg2” using “pip” (the python package manager) I got an error:

$ pip install psycopg2
:
Error: pg_config executable not found.
:

I just searched for “pg_config” in my system:

$ find / -name pg_config  2>/dev/null
/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Then added to my PATH env and pip worked:

$ export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/
$ pip install psycopg2

Rsnapshot on OSX

Rsnapshot is a backup solution for Unix machines including Linux and OSX, it supports many great features including full backups with the size of only incremental backup, it also support backing up local and remote machines.

Check my previous post for more details about it, Backup Journey to rsnapshot

Install Rsnapshot

The easiest way to install unix programs in OSX is to use Homebrew, after installing Homebrew install rsnapshot and needed programs:

$ brew update
$ brew install rsnapshot
$ brew install coreutils

“coreutils” is needed because “cp” command installed with OSX doesn’t support GNU “cp” options needed for rsnapshot.

Configuration

I will be running rsnapshot from user account and not root user, I will be using my user “rayed”:

$ mkdir /Users/rayed/rsnapshot
$ cat > /Users/rayed/rsnapshot/rsnapshot.conf << EOF
#################################################
# rsnapshot.conf - rsnapshot configuration file #
#################################################
#                                               #
# PLEASE BE AWARE OF THE FOLLOWING RULES:       #
#                                               #
# This file requires tabs between elements      #
#                                               #
# Directories require a trailing slash:         #
#   right: /home/                               #
#   wrong: /home                                #
#                                               #
#################################################
config_version	1.2

snapshot_root	/Users/rayed/rsnapshot/

cmd_cp		/usr/local/bin/gcp
cmd_rm		/bin/rm
cmd_rsync	/usr/bin/rsync
cmd_ssh		/usr/bin/ssh
cmd_logger	/usr/bin/logger

retain		hourly	4
retain		daily	7
retain		weekly	4
retain		monthly	3

verbose		2
loglevel	3
logfile		/Users/rayed/rsnapshot/rsnapshot.log
lockfile	/Users/rayed/rsnapshot.pid

# Backup local machine
backup		/Users/rayed/	localhost/
# Backup remote machine
backup		rayed@example.com:/home/rayed/	example.com/
EOF

Important notes about the configuration files:

  • Use TABs not spaces between elements.
  • Directories require a trailing slash, i.e. /home/ not /home
  • On OSX we replaced “cmd_cp” from “/bin/cp” to “/usr/local/bin/gcp” to support rsnapshot options.
  • For remote machine backup, make sure you use ssh keys, Rsnapshot can’t use passwords.

Running rsnapshot

To test your setup try the following command:

rsnapshot -c /Users/rayed/rsnapshot/rsnapshot.conf hourly

When everything works fine, you need to configure “rsnapshot” to run periodically from cron, install the following line:

$ crontab -e
:
# RSnapshot
0 */6 * * *	/usr/local/bin/rsnapshot -c /Users/rayed/rsnapshot/rsnapshot.conf hourly
30 3 * * *	/usr/local/bin/rsnapshot -c /Users/rayed/rsnapshot/rsnapshot.conf daily
0  3 * * 1	/usr/local/bin/rsnapshot -c /Users/rayed/rsnapshot/rsnapshot.conf weekly
30 2 1 * *	/usr/local/bin/rsnapshot -c /Users/rayed/rsnapshot/rsnapshot.conf monthly

After few hours double check your setup by making reviewing the log file “/Users/rayed/rsnapshot/rsnapshot.log”, and check the rsnapshot root directory for your backups, you should have something like:

$ ls -l /Users/rayed/rsnapshot/
hourly.0
hourly.1
hourly.2
hourly.3
daily.0
daily.1
:

Install Python Image Library (PIL) on OSX

PIL or Python Imaging Library is a library that allows you to manipulate images in Python programming language, trying to install “PIL” using “pip” tool won’t work perfectly so here is how to proper installation.

Install Brew

Brew is a package management system that simplifies the installation of software on Mac OS X, you can install it easily with the following command:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Install Pil

Actually we will install ”pillow” a fork of ”pil”:

brew install samueljohn/python/pillow

Configure your Python Path

To make PIL available to Python we add its path to PYTHONPATH variable:

vi ~/.bash_profile
PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH

Check it

To test if you have PIL installed:

python -c "import PIL.Image"

You shouldn’t get any errors.

Python auto complete in OSX

If you run Python shell in OSX you notice the auto completion functionality isn’t working, this is caused by Apple decision not to ship GNU readline and instead they use libedit (BSD license), to fix the problem I used the following snippet:

import readline
import rlcompleter
if 'libedit' in readline.__doc__:
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")

Source:
http://stackoverflow.com/a/7116997

Fixing “cannot change locale (UTF-8)” error

I am trying new way to fix the annoying “cannot change locale (UTF-8)” error, this error usually show when you login from your OSX to a Linux machine:

osx$ ssh ubuntu-server
Last login: Sat Mar  9 09:04:40 2013 from 10.10.12.16
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)
[rayed@ubuntu-server ~]$

The problem happens because OSX send LC_CTYPE environment variable when you connect through ssh, the solution is to ask OSX not to send these variables. This can be done by editing you ssh config file on you OSX and commenting the “SendEnv” variable:

    
osx$ sudo vi /etc/ssh_config
:
#SendEnv LANG LC_*
:

My first Git Session

This guide for my own reference please don’t rely on it, for a better introduction check out Git Reference website. Basic concept and terminologies, Git have 3 logical places:

  • Working Area: normal directory where you edit your project.
  • Staging Area: Think of the staging as your project state before you start your coding session, after each coding session you add your changes to staging area using “git add filename” or “git add .”
  • Snapshot Area: After multiple coding sessions you would finish a logical change, e.g. adding commenting system in web site, next you need to store a snapshot of your project after these changes, you would do that with “git commit”

Setting the environment:

git config --global user.name "Rayed Alrashed"
git config --global user.email "my@email"
git config --global color.ui true

Start versioning a project:

cd my_project
make clean
vi .gitignore  # files you want Git to ignore, e.g. log, tmp, binaries
git init           # Initalize Git 
git add .        # Add All files to Git tracking
git commit    # Initial commit, get a snapshot of the project

Typical workflow:

cd my_project
:
vi README
git status -s            # Get the status of files changed
git add README      # Add README to staging area
:
git commit -m "Describe your change"        # Make a snapshot of the staging area