Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Wednesday, February 11, 2015

Connecting Django to Oracle database using service name

These are steps for connecting Django to Oracle database using service name. It is tested with Django 1.7 running on Ubuntu 14.04.
  1. Ensure that cx_oracle is installed (Please see the References section for details).
  2. Locate/ create file tnsnames.ora
    In case Oracle DB or client is installed, you can try to locate the file under ORACLE_HOME/network/admin/
  3. Add an entry for the target host and service name to tnsnames.ora
    Example:
  4. Ensure that TNS_ADMIN environment variable is set to the folder containing tnsnames.ora
  5. Change the database connection in Django settings file to
  6. Now, you are ready to connect to Oracle db.
References:
  • http://codeforaliving.blogspot.com/2015/02/installing-cxoracle-on-ubuntu_73.html
  • http://www.orafaq.com/wiki/Tnsnames.ora
  • https://docs.djangoproject.com/en/1.7/ref/databases/ (Please see the section related to Oracle)

Installing cx_oracle on Ubuntu

The following instructions were tested on Ubuntu 14.04. The main challenge is installing Oracle Instant Client. It is assumed that no Oracle DB or client were previously installed. Otherwise, you can install cx_oracle with pip straight away.
  1. Download oracle instant client both basic and sdk zipped packages from http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html. Example:
    instantclient-basic-linux.x64-12.1.0.2.0.zip
    instantclient-sdk-linux.x64-12.1.0.2.0.zip
  2. Create folder /opt/oracle
    sudo mkdir -p /opt/oracle
  3. Unzip both the basic and sdk packages to /opt/oracle
    sudo unzip instantclient-basic-linux.x64-12.1.0.2.0.zip -d /opt/oracle
    sudo unzip instantclient-sdk-linux.x64-12.1.0.2.0.zip -d /opt/oracle
  4. Set ORACLE_HOME and LD_LIBRARY_PATH to unzipped folder. You can choose to
    • Use export command
      export ORACLE_HOME=/opt/oracle/instantclient_12_1
      export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_1
    • Put the variables into /etc/environment (You may need to log out from the shell and log in again for the settings to take effect)
      ORACLE_HOME=/opt/oracle/instantclient_12_1
      LD_LIBRARY_PATH=/opt/oracle/instantclient_12_1
  5. Now you can use pip to install cx_oracle
    pip install cx_oracle
References: