Connecting to an Oracle RDS instance from Python
- Create an RDS instance on AWS. you can follow the steps here to create one.
- Install the python library that connects to an Oracle database.
pip install cx_Oracle
- The cx_Oracle package requires Oracle Instant Client to connect to the database. So download the Oracle Instant Client for windows from here.
- Install Oracle Client libraries, make sure it matches your python installation (64bit/32bit).
- Unzip Instant Client into a directory.
- Add Environment Variable Path pointing to this directory.
- Restart Your System for the path variable changes to take affect.
Now that you have all the tools required. Open up python and follow the code below to connect to an RDS and start querying.
import pandas as pd#Connect to Database
#connstr = cx_Oracle.connect("user/password@server/ServiceName")connstr = "admin/password@connection:1521/orcl"
connection = cx_Oracle.connect(connstr)
cursor = connection.cursor()#Execute Query
cursor.execute("select * from db")
result = cursor.fetchall()#Fetch results
for row in result:
print(row)#Transform Query results into a Pandas Dataframe
df = pd.DataFrame(result)
df.columns=[row for row in cursor.description]#Commit Changes
connection.commit()#Close Cursor and Connection
Documentation of the cx_Oracle library: