How to connect Python to Oracle using oracledb

919 views Asked by At

I'm trying to connect to Oracle using using the following Python script:

import oracledb  
connection = oracledb.connect(user = 'user', 
                                  password = 'PW', 
                                  service_name = 'xxx') 

However, I am getting the following error:

OperationalError: DPY-6005: cannot connect to database (CONNECTION_ID=KKNOzerEHqa1cFi8jN5yWw==). [WinError 10061] No connection could be made because the target machine actively refused it

If I had to guess I am putting in the incorrect service_name. I know what the TNS service name is and Database service names are, but I'm not sure if these are what I need to enter. Provided these are correct, its not clear what exactly I would enter here, e.g. service_name = 'local/service name' or simply just enter the service name excluding 'local/'.

Finally, I usually connect to my work database using a JDBC custom url connection e.g. "jdbc:oracle:thin:@ldap://ldaptns...". Does anyone know if it possible to utilise this in oracledb.connect()?

Thanks

1

There are 1 answers

0
Anthony Tuininga On

You need to specify the host, as John Gordon noted. Like this:

import oracledb

connection = oracledb.connect(
    user="user",
    password="PW",
    host="server_host",
    port=1521,       # this is optional
    service_name="xxx"
)

# or you can use this (Easy Connect string)

connection = oracledb.connect(
    user="user",
    password="PW",
    dsn="server_host:server_port/xxx"
)