Oracle (RDS) + CentOS 5.8 (EC2) + Python (cx_Oracle)

For anyone looking to have Python (CentOS 5.8) connect to an Oracle instance (RDS):

$ yum install python26 python26-devel (I was using a Rightscale instance that already had the EPEL repository loaded, otherwise see: http://fedoraproject.org/wiki/EPEL)

$ wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086

$ sh setuptools-0.6c11-py2.6.egg

$ easy_install-2.6 pip

$ pip-2.6 install virtualenvwrapper


Add to /etc/profile

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.6
. /usr/bin/virtualenvwrapper.sh
 
export ORACLE_HOME=/opt/oracle/instantclient_11_2
export ORACLE_BASE=/opt/oracle/instantclient_11_2
export TNS_ADMIN=/opt/oracle/instantclient_11_2
export ORACLE_HOME=/opt/oracle/instantclient_11_2
export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2
 
$ mkdir -p /opt/oracle
 
$ unzip instantclient-basic-linux.x64-11.2.0.3.0.zip -d /opt/oracle

$ cd /opt/oracle/instant*

$ ln -s libclntsh.so.11.1 libclntsh.so
 
$ rpm -i http://prdownloads.sourceforge.net/cx-oracle/cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm?download
$ mkvirtualenv oracle –system-site-packages


Create /opt/oracle/instantclient112/tnsnames.ora

TOYBOX =
(DESCRIPTION =
(ADDRESSLIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = X.Y.us-east-1.rds.amazonaws.com)(PORT = 1521))
)
(CONNECT
DATA =
(SERVICE_NAME = ORCL)
)
)

 

Connect via Python

(oracle)[root@domU-12-31-39-16-36-60 python-connect]# python2.6

Python 2.6.8 (unknown, Apr 12 2012, 20:59:36)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import cx_Oracle

connection = cxOracle.connect(‘USERNAME/PASSWORD@TOYBOX’)
cursor = connection.cursor()
cursor.execute(“SELECT count(*) FROM sys.dba_tab_privs WHERE grantee=’PUBLIC'”)
<__builtin__.OracleCursor on <cx
Oracle.Connection to ajordens@TOYBOX>>
count = cursor.fetchall()[0][0]

print count
2494

cursor.close()
connection.close()