Wellwoah
Wellwoah

Reputation: 79

'TNS:could not resolve the connect identifier specified' when connecting to oracle instance through cx_Oracle

I have an Oracle instance on a AWS EC2 instance. When I run this in the AWS linux terminal, it is successful.

ubuntu@ip-xxx-xx-xx-xxx:~$ sqlplus user101/pass101@ip-xxx-xx-xx-xxx:1521/XE

enter image description here

However, when I run the same arguments when connecting through cx_Oracle I get a:

"ORA-12154: TNS:could not resolve the connect identifier specified" error

enter image description here

I know some common problems are with the tnsnames.ora file. I've placed that below.

XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ip-172-xx-xx-xx)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )

~       

Upvotes: 0

Views: 268

Answers (1)

Alex Poole
Alex Poole

Reputation: 191425

At the moment the full Easy Connect syntax is being interpreted as a TNS entry.

According to the Connect() documentation, you can use your current code if you specify that you are passing the user argument:

If the user parameter is passed and the password and dsn parameters are not passed, the user parameter is assumed to be a connect string in the format user/password@dsn, the same format accepted by Oracle applications such as SQL*Plus.

So this should work:

connection = cx_oracle.Connect(user="user101/pass101@ip-172-xx-xx-xx:1521/XE")

According to the Oracle connection handling documentation, you could also split that out:

connection = cx_oracle.Connect(
                 user="user101",
                 password="pass101",
                 dsn="ip-172-xx-xx-xx:1521/XE",
                 encoding="UTF-8"
             )

Or if you want to use tnsnames.ora then just give that alias as the DSN value:

connection = cx_oracle.Connect(
                 user="user101",
                 password="pass101",
                 dsn="XE",
                 encoding="UTF-8"
             )

Upvotes: 1

Related Questions