pythonoracle-databasepython-oracledb

What does 'nodename nor servname provided, or not known' mean with python-oracledb?


For example, using Python 3.9.6 and connecting or creating a connection pool in python-oracledb's default 'Thin' mode:

import oracledb
import os

un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "doesnotexist.oracle.com/orclpdb1"

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

gives this error on macOS:

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

On Linux the error is slightly different:

socket.gaierror: [Errno -2] Name or service not known

What does it mean?


Solution

  • It means the hostname (or IP address) given can't be reached.

    The solution is to find the correct hostname (or IP address) where the database is located. Check for typos in the connection string you used. Pass a valid hostname during connection creation, for example:

    cs = "validhost.oracle.com/orclpdb1"
    

    or you could use:

    connection = oracledb.connect(user=u, password=p,
                                  host="validhost.oracle.com", service_name="orclpdb1")
    

    The same error and solution also apply to connection pool creation.

    Make sure the database host is reachable from the machine where Python is running.