Oracle (RDS) + CentOS 5.8 (EC2) + Python (cx_Oracle)
09 Jul 2012For 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)
$ sh setuptools-0.6c11-py2.6.egg
$ easy_install-2.6 pip
$ pip-2.6 install virtualenvwrapper
Add to /etc/profile
Create /opt/oracle/instantclient112/tnsnames.ora
TOYBOX =
(DESCRIPTION =
(ADDRESSLIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = X.Y.us-east-1.rds.amazonaws.com)(PORT = 1521))
)
(CONNECTDATA =
(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 <cxOracle.Connection to ajordens@TOYBOX>>
count = cursor.fetchall()[0][0]print count
2494cursor.close()
connection.close()