Oracle setup
Overview of dbt-oracle
- Maintained by: Oracle
- Authors: Oracle
- GitHub repo: oracle/dbt-oracle
- PyPI package:
dbt-oracle
- Slack channel: #db-oracle
- Supported dbt Core version: v1.2.1 and newer
- dbt Cloud support: Not Supported
- Minimum data platform version: SQlite Version 3.0
Installation
Installing dbt-oracle
pip is the easiest way to install the adapter:
pip install dbt-oracle
Installing dbt-oracle
will also install dbt-core
and any other dependencies.
Configuring dbt-oracle
For Oracle-specifc configuration please refer to Oracle Configuration
For further info, refer to the GitHub repository: oracle/dbt-oracle
Configure the Python driver mode
python-oracledb is the renamed, major release of Oracle's popular cx_Oracle interface
python-oracledb makes it optional to install the Oracle Client libraries. This driver supports 2 modes
- Thin mode (preferred) : Python process directly connects to the Oracle database. This mode does not need the Oracle Client libraries
- Thick mode : Python process links with the Oracle Client libraries. Some advanced Oracle database functionalities (for e.g. Advanced Queuing and Scrollable cursors) are currently available via Oracle Client libraries
It is highly recommended to use the thin mode as it vastly simplifies installation. You can configure the driver mode using the environment variable ORA_PYTHON_DRIVER_TYPE
Driver Mode | Oracle Client libraries required? | Configuration |
---|---|---|
Thin | No | ORA_PYTHON_DRIVER_TYPE=thin |
Thick | Yes | ORA_PYTHON_DRIVER_TYPE=thick |
cx_oracle (old driver) | Yes | ORA_PYTHON_DRIVER_TYPE=cx |
The default value of ORA_PYTHON_DRIVER_TYPE
is cx
. This might change in the future as more users migrate towards the new python driver.
- Thin
- Thick
export ORA_PYTHON_DRIVER_TYPE=thin
export ORA_PYTHON_DRIVER_TYPE=thick
# or
export ORA_PYTHON_DRIVER_TYPE=cx # default
Install Oracle Instant Client libraries
In thick mode or the old cx_oracle mode, you will need the Oracle Instant Client libraries installed. These provide the necessary network connectivity allowing dbt-oracle to access an Oracle Database instance.
Oracle client libraries versions 21, 19, 18, 12, and 11.2 are supported where available on Linux, Windows and macOS (Intel x86). It is recommended to use the latest client possible: Oracle’s standard client-server version interoperability allows connection to both older and newer databases.
- Linux
- Windows
- MacOS
Download an Oracle 21, 19, 18, 12, or 11.2 “Basic” or “Basic Light” zip file matching your Python 64-bit or 32-bit architecture:
Unzip the package into a single directory that is accessible to your application. For example:
mkdir -p /opt/oracle
cd /opt/oracle
unzip instantclient-basic-linux.x64-21.1.0.0.0.zipInstall the libaio package with sudo or as the root user. For example:
sudo yum install libaio
On some Linux distributions this package is called
libaio1
instead.
if there is no other Oracle software on the machine that will be impacted, permanently add Instant Client to the runtime link path. For example, with sudo or as the root user:
sudo sh -c "echo /opt/oracle/instantclient_21_1 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfigAlternatively, set the environment variable
LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/oracle/instantclient_21_1:$LD_LIBRARY_PATH
- Download an Oracle 21, 19, 18, 12, or 11.2 “Basic” or “Basic Light” zip file: 64-bit or 32-bit, matching your Python architecture.
Note that Oracle Client versions 21c and 19c are not supported on Windows 7.
Unzip the package into a directory that is accessible to your application. For example unzip
instantclient-basic-windows.x64-19.11.0.0.0dbru.zip
toC:\oracle\instantclient_19_11
.Oracle Instant Client libraries require a Visual Studio redistributable with a 64-bit or 32-bit architecture to match Instant Client’s architecture.
- For Instant Client 21 install VS 2019 or later
- For Instant Client 19 install VS 2017
- For Instant Client 18 or 12.2 install VS 2013
- For Instant Client 12.1 install VS 2010
- For Instant Client 11.2 install VS 2005 64-bit
Add the Oracle Instant Client directory to the
PATH
environment variable.The directory must occur inPATH
before any other Oracle directories. Restart any open command prompt windows.SET PATH=C:\oracle\instantclient_19_9;%PATH%
Download the instant client DMG package
cd $HOME/Downloads
curl -O https://download.oracle.com/otn_software/mac/instantclient/198000/instantclient-basic-macos.x64-19.8.0.0.0dbru.dmgMount the instant client DMG package
hdiutil mount instantclient-basic-macos.x64-19.8.0.0.0dbru.dmg
Run the install script in the mounted package
/Volumes/instantclient-basic-macos.x64-19.8.0.0.0dbru/install_ic.sh
Unmount the package
hdiutil unmount /Volumes/instantclient-basic-macos.x64-19.8.0.0.0dbru
The Instant Client directory will be
$HOME/Downloads/instantclient_19_8
. You could move it to some place convenient.Add links to
~/lib
or/usr/local/lib
to enable dbt to find the libraries.mkdir ~/lib
ln -s ~/instantclient_19_8/libclntsh.dylib ~/lib/
Configure wallet for Oracle Autonomous Database in Cloud
dbt can connect to Oracle Autonomous Database (ADB) in Oracle Cloud using either TLS (Transport Layer Security) or mutual TLS (mTLS). TLS and mTLS provide enhanced security for authentication and encryption. A database username and password is still required for dbt connections which can be configured as explained in the next section Connecting to Oracle Database.
- TLS
- Mutual TLS
With TLS, dbt can connect to Oracle ADB without using a wallet. Both Thin and Thick modes of the python-oracledb driver support TLS.
In Thick mode, dbt can connect through TLS only when using Oracle Client library versions 19.14 (or later) or 21.5 (or later).
Refer to Oracle documentation to connect to an ADB instance using TLS authentication and the blog post Easy wallet-less connections to Oracle Autonomous Databases in Python to enable TLS for your Oracle ADB instance.
For mutual TLS connections, a wallet needs be downloaded from the OCI console and the python driver needs to be configured to use it.
Install the Wallet and Network Configuration Files
From the Oracle Cloud console for the database, download the wallet zip file using the DB Connection
button. The zip contains the wallet and network configuration files.
Keep wallet files in a secure location and share them only with authorized users.
Unzip the wallet zip file.
- Thin
- Thick
tnsnames.ora
- Maps net service names used for application connection strings to your database servicesewallet.pem
- Enables SSL/TLS connections in Thin mode. Keep this file secure
After unzipping the files in a secure directory, set the TNS_ADMIN and WALLET_LOCATION environment variables to the directory name.
export WALLET_LOCATION=/path/to/directory_containing_ewallet.pem
export WALLET_PASSWORD=***
export TNS_ADMIN=/path/to/directory_containing_tnsnames.ora
Optionally, if ewallet.pem
file is encrypted using a wallet password, specify the password using environment variable WALLET_PASSWORD
tnsnames.ora
- Maps net service names used for application connection strings to your database servicessqlnet.ora
- Configures Oracle Network settingscwallet.sso
- Enables SSL/TLS connections
After unzipping the files in a secure directory, set the TNS_ADMIN environment variable to that directory name.
export TNS_ADMIN=/path/to/directory_containing_tnsnames.ora
Next, edit the sqlnet.ora
file to point to the wallet directory.
WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY="/path/to/wallet/directory")))
SSL_SERVER_DN_MATCH=yes
Connecting to Oracle Database
Define the following mandatory parameters as environment variables and refer them in the connection profile using env_var jinja function. Optionally, you can also define these directly in the profiles.yml
file, but this is not recommended
export DBT_ORACLE_USER=<username>
export DBT_ORACLE_PASSWORD=***
export DBT_ORACLE_SCHEMA=<username>
Starting with dbt-oracle==1.0.2
, it is optional to set the database name
export DBT_ORACLE_DATABASE=example_db2022adb
If database name is not set, adapter will retrieve it using the following query.
SELECT SYS_CONTEXT('userenv', 'DB_NAME') FROM DUAL
An Oracle connection profile for dbt can be set using any one of the following methods
- Using TNS alias
- Using Connect string
- Using Database hostname
The tnsnames.ora
file is a configuration file that contains network service names mapped to connect descriptors.
The directory location of tnsnames.ora
file can be specified using TNS_ADMIN
environment variable
db2022adb_high = (description =
(retry_count=20)(retry_delay=3)
(address=(protocol=tcps)
(port=1522)
(host=adb.example.oraclecloud.com))
(connect_data=(service_name=example_high.adb.oraclecloud.com))
(security=(ssl_server_cert_dn="CN=adb.example.oraclecloud.com,
OU=Oracle BMCS US,O=Oracle Corporation,L=Redwood City,ST=California,C=US")))
The TNS alias db2022adb_high
can be defined as environment variable and referred in profiles.yml
export DBT_ORACLE_TNS_NAME=db2022adb_high
dbt_test:
target: dev
outputs:
dev:
type: oracle
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
tns_name: "{{ env_var('DBT_ORACLE_TNS_NAME') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
threads: 4
The connection string identifies which database service to connect to. It can be one of the following
- An Oracle Easy Connect String
- An Oracle Net Connect Descriptor String
- A Net Service Name mapping to a connect descriptor
export DBT_ORACLE_CONNECT_STRING="(description=(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)
(host=adb.example.oraclecloud.com))(connect_data=(service_name=example_high.adb.oraclecloud.com))
(security=(ssl_server_cert_dn=\"CN=adb.example.oraclecloud.com,
OU=Oracle BMCS US,O=Oracle Corporation,L=Redwood City,ST=California,C=US\")))"
dbt_test:
target: "{{ env_var('DBT_TARGET', 'dev') }}"
outputs:
dev:
type: oracle
user: "{{ env_var('DBT_ORACLE_USER') }}"
pass: "{{ env_var('DBT_ORACLE_PASSWORD') }}"
database: "{{ env_var('DBT_ORACLE_DATABASE') }}"
schema: "{{ env_var('DBT_ORACLE_SCHEMA') }}"
connection_string: "{{ env_var('DBT_ORACLE_CONNECT_STRING') }}"
To connect using the database hostname or IP address, you need to specify the following
- host
- port (1521 or 1522)
- protocol (tcp or tcps)
- service
export DBT_ORACLE_HOST=adb.example.oraclecloud.com
export DBT_ORACLE_SERVICE=example_high.adb.oraclecloud.com
Supported Features
- Table materialization
- View materialization
- Incremental materialization
- Seeds
- Data sources
- Singular tests
- Generic tests; Not null, Unique, Accepted values and Relationships
- Operations
- Analyses
- Exposures
- Document generation
- Serve project documentation as a website
- All dbt commands are supported
Not Supported features
- Ephemeral materialization