How to install psycopg2 on Linux

psycopg2 is a Python module for PostgreSQL. Various PostgreSQL and Python scripts require this. It can be a bit tricky to install. So here is how I was able to install psycopg2 on CentOS 5.x. PIP/Source installs should be applicable for any Linux OS.

Requirements:
– Postgresql
– Postqresql-devel
– Python

YUM Installation:
yum install python-psycopg2

Apt-Get Installation:
apt-get install python-psycopg2

PIP Installation:
PIP – A tool for installing and managing Python packages. This is required and is needed to install Python packages with pip. Clone pip from github:
cd ~
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py –no-check-certificate
python get-pip.py

Once pip is installed, then locate and export the directory that contains pg-config. X.Y specific to your pgsql install:
locate pg_config
export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH

Now to install using pip:
pip install psycopg2

Source Installation:
Locate and export the directory that contains pg-config. X.Y specific to your pgsql install:
locate pg_config
export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH

Now to install psycopg2 via source:
cd ~
wget http://initd.org/psycopg/tarballs/PSYCOPG-2-5/psycopg2-2.5.3.tar.gz
tar zxvf psycopg2-2.5.3.tar.gz
cd psycopg2-2.5.3/
python setup.py install

Please contact me if you have any issues. Enjoy!