Skip to content
ohsudo, Home
Databases

Oracle "ORA-12154: TNS:could not resolve the connect identifier specified": how to fix it

Fix Oracle ORA-12154: the client can't find your connect identifier. Check tnsnames.ora, TNS_ADMIN and sqlnet.ora, an @ in the password, or use Easy Connect.

Intermediate4 min readApplies to Oracle Database 19c · 21c · 23ai · Oracle Client · Instant Client · SQL*Plus

by OhSudo ··

Not sure this is your cause?Find it step by step: Can't connect to the database →

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

Newer clients word it differently, and tell you more:

ORA-12154: Cannot connect to database. Could not find alias PRODDB in /opt/oracle/network/admin/tnsnames.ora.

This is the client’s error. The connection hasn’t left your machine: the client was given a name like PRODDB, and couldn’t work out which server and service it means. So the database, the listener and the firewall aren’t the problem yet. Look at your client’s configuration.

  1. Clientconnect to PRODDB
  2. tnsnames.orathe one this client reads: no PRODDB(breaks here)
  3. Database servernever contacted
This is the client's error: the connection never left your machine. It was given a name like PRODDB and couldn't find which server and service it means.

TL;DR

Terminal window
tnsping PRODDB # which files it reads, and what it finds
echo $TNS_ADMIN # the folder that should hold tnsnames.ora
sqlplus app@//db01.example.com:1521/PROD # Easy Connect: no tnsnames.ora needed

If that last one connects, the database is fine and your problem is in tnsnames.ora, or in where the client looks for it.

1. Skip the file: try Easy Connect

Name the server and service directly. It’s the quickest test:

Terminal window
sqlplus app@//db01.example.com:1521/PROD

The format is //host:port/service_name. If it works, keep reading to fix the alias, or just use this form in your application. Note that PROD is the service name, not necessarily the SID.

A different error here means you got past ORA-12154, and the rest of the path is what’s wrong (see the end of this article).

2. Which tnsnames.ora does the client read?

Terminal window
tnsping PRODDB
Used parameter files:
/opt/oracle/network/admin/sqlnet.ora
TNS-03505: Failed to resolve name

The client looks for tnsnames.ora in this order:

  1. the folder in the TNS_ADMIN environment variable;
  2. network/admin in the Oracle home;
  3. and for Instant Client, its own network/admin subfolder.

So the file you’re editing may simply not be the one being read. Set it explicitly:

Terminal window
export TNS_ADMIN=/opt/oracle/network/admin

On Windows, set it in System Properties → Environment Variables, and restart the application. tnsping comes with the full client; with Instant Client you may not have it, and Easy Connect from section 1 is the quickest test.

3. Is the entry right?

A correct entry:

PRODDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db01.example.com)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = PROD)
)
)

Things that break it:

  • The alias differs from what you typed. PRODDB in the file and PROD_DB in the connection string don’t match.
  • A domain is added. With NAMES.DEFAULT_DOMAIN = example.com in sqlnet.ora, the client looks for PRODDB.example.com. Remove that line, or add the domain to the alias.
  • Unbalanced brackets, or a stray character from a copy and paste. One missing ) can invalidate the entries after it.
  • The alias doesn’t start at the beginning of the line. Leading spaces can make the parser treat it as a continuation of the previous entry.
  • Wrong encoding or a wrong line ending from an editor, more rarely.

4. sqlnet.ora may turn the file off

sqlnet.ora decides where names are looked up:

NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)

If TNSNAMES isn’t in that list, your file is ignored. And if EZCONNECT is missing, the //host:port/service form fails with this same error.

5. An @ in the password

A classic one. The client splits the connection string at @:

Terminal window
sqlplus app/P@ssw0rd@PRODDB # the client reads "ssw0rd@PRODDB" as the identifier: ORA-12154

Quote the password, and let your shell pass the quotes through:

Terminal window
sqlplus 'app/"P@ssw0rd"@PRODDB'

Better still, leave the password out and let the tool ask for it: sqlplus app@PRODDB. It also won’t end up in your shell history. In a JDBC or ODBC URL, the password goes in its own field.

6. On Windows: several Oracle homes, 32 and 64 bits

A machine with more than one Oracle client may use a different one from what you think. where sqlplus shows which is first in PATH. A 32-bit application needs the 32-bit client, with its own tnsnames.ora, even on 64-bit Windows; this catches a lot of Excel, Power BI and older tools. Setting TNS_ADMIN makes them all read the same file.

Shell, we have a problem!

Similar, but different: these come after the name was resolved, so the problem has moved. ORA-12541: no listener: nothing listens on that host and port, so check lsnrctl status, and the Port Lookup entry for 1521. ORA-12514: listener does not currently know of service: the listener is up, and the service name is wrong or not registered. ORA-12170: connect timeout: a firewall, see “Connection timed out” vs “Connection refused”. ORA-01017: invalid username/password: you reached the database. Once you’re connected, a missing table is ORA-00942. If the alias resolves and nothing answers, see ORA-12541.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles