On macOS with Python 3.9.6 the Python code using Oracle's python-oracledb driver:

import oracledb
import os

un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/orclpdb1"

c = oracledb.connect(user=un, password=pw, dsn=cs)

gives the error:

DPY-6005: cannot connect to database. Connection failed with "[Errno 61] Connection refused"

on Linux the error is like:

DPY-6005: cannot connect to database. Connection failed with "[Errno 111] Connection refused"

What do these mean?

[Update: in python-oracledb 1.0.1 the error is wrapped with DPY-6005. In 1.0.0 just the lower level Python part of the error was shown.]

2

There are 2 best solutions below

1
On
  • One scenario is that the database port you used (or the default port 1521) is not correct. Find the correct port and use that instead. For example if your database listener is listening on port 1530 instead of the default port 1521, then you could try the connection string:

    cs = "localhost:1530/orclpdb1"
    
  • Make sure the hostname is correct: you may have connected to the wrong machine.

0
On

In my experience, "connection refused" often means that the connection was actively denied, which could mean that the database is protected by a firewall. If you have already confirmed the hostname and port are correct and are still getting this error then determine if there is a firewall, either on the database server itself or elsewhere on the network, and have a rule created to allow access or disable it entirely (assuming it is safe to do so).