Skip to content

ORA-12154: TNS Could Not Resolve the Connect Identifier

ORA-12154 never reached the database. Your client couldn't turn the alias into an address. Here's how to find which tnsnames.ora it actually read, why TNS_ADMIN differs per client, and when to drop the alias for EZConnect.

ora-12154 tnsnames tns-admin sqlnet-ora oracle-clients oracle-net oracle-database
Gopi Gorantala
Reading Progress

On This Page

SQL Developer connected to FXRATES without complaint. Same laptop, same user, same alias, and sqlplus fx_loader@FXRATES came back with ORA-12154 before I'd even finished typing the password. That detail is the whole diagnosis, though it took me a while to see it. Two programs, two different ideas of where tnsnames.ora lives. The database was never involved.

Here's what it looks like

A 19c-era client prints the version everyone has searched for at least once:

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

Newer clients finally tell you what they were looking for, and where. Oracle's error help page for ORA-12154 gives the template, and the 26ai Net Services Administrator's Guide shows it filled in:

ORA-12154: Cannot connect to database. Cannot find alias alias_value in repository_loc.

ORA-12154: Cannot connect to database.
Could not find alias sales in
/u01/app/oracle/product/21.3.0/dbhome_1/network/admin/tnsnames.ora.

The second form is a gift. It names the file. If you see a path in there you didn't expect, you've already found the bug. The guide also lists variants ending in and LDAP or naming two files (a system one and a user one), so read the whole tail of the message, not only the code.

python-oracledb in thin mode has its own codes for the same situation, because thin mode does the lookup itself instead of handing it to Oracle's client libraries:

DPY-4027: no configuration directory specified
DPY-4000: unable to find "FXRATES" in /home/gopi/lab12154/b/tnsnames.ora

DPY-4027 is from the python-oracledb troubleshooting guide. The DPY-4000 shape comes from reports like this python-oracledb discussion; the alias and path are mine.

It shows up at connect time, instantly. No timeout. That's a clue in itself.

It never left your machine

ORA-12154 is raised by the client's naming layer, before any socket opens. No listener saw your request. So restarting the database can't matter, and neither can the firewall.

Compare it to its neighbours. ORA-12541 means the name resolved and nothing answered on that host and port (I wrote that one up here). ORA-12514 means a listener answered and didn't know your service (that one's here). ORA-12154 is one step earlier than both. If you want the three side by side, there's a comparison.

Two minutes of checks, all on the client machine:

echo "$TNS_ADMIN"                          # empty is a valid, and common, answer
ls -l "$TNS_ADMIN"/tnsnames.ora            # does the file you think is used even exist?
grep -n -i '^FXRATES' "$TNS_ADMIN"/tnsnames.ora   # is the alias there, starting in column 1?
tnsping FXRATES                            # full client only; resolves the alias, never logs in
sqlplus fx_loader@//db-fx:1521/FXPDB1      # skip naming entirely with EZConnect

tnsping walks the same naming methods your client would, and its first lines say which sqlnet.ora it used and which adapter resolved the alias. It isn't in the Basic Instant Client package, so on a slim client lean on the EZConnect test. If that login gets further, your problem is purely the alias.

My first wrong instinct, for the record: I assumed the alias was misspelled and spent a while diffing tnsnames.ora against the connect string. The file was perfect. sqlplus wasn't reading it.

Reproduce it without a database

You don't need Oracle running for this one. ORA-12154 happens before the network, so python-oracledb or an Instant Client with SQL*Plus is enough.

One honest note. I couldn't run this in the sandbox I used for this write-up (no route to PyPI or Maven Central, no Docker daemon), so there's no captured output below. The messages come from Oracle's and python-oracledb's docs.

You need pip install oracledb (thin mode, no Oracle client required), and optionally an Instant Client with SQL*Plus for the thick-client half.

Set up two config directories. One has the alias, one doesn't:

mkdir -p ~/lab12154/a ~/lab12154/b

cat > ~/lab12154/a/tnsnames.ora <<'EOF'
FXRATES =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db-fx)(PORT = 1521))
    (CONNECT_DATA = (SERVICE_NAME = FXPDB1)))
EOF

cat > ~/lab12154/b/tnsnames.ora <<'EOF'
LEDGER =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db-ledger)(PORT = 1521))
    (CONNECT_DATA = (SERVICE_NAME = LEDGERPDB1)))
EOF

The FX rates loader, cut down to the one line that matters:

import oracledb

conn = oracledb.connect(user="fx_loader", password="not-the-point",
                        dsn="FXRATES")  # an alias: thin mode must find tnsnames.ora itself

Now the steps:

  1. unset TNS_ADMIN ORACLE_HOME; python3 lab.py. Expected: DPY-4027: no configuration directory specified. Thin mode had an alias and nowhere to look.
  2. TNS_ADMIN=~/lab12154/b python3 lab.py. Expected: DPY-4000, naming FXRATES and ~/lab12154/b/tnsnames.ora. Right mechanism, wrong file. This is the one that bites in real life.
  3. TNS_ADMIN=~/lab12154/a python3 lab.py. Expected: the error changes layer, to a DPY-6005 "cannot connect to database" about reaching db-fx, a host that doesn't exist. That change confirms the alias resolved.
  4. Break the file without touching the alias. Edit ~/lab12154/a/tnsnames.ora and put one space before FXRATES =. Run step 3 again and the alias is gone. Oracle's config syntax rules say a line that doesn't start in the far-left column is a continuation of the previous one, so your entry got glued onto nothing.
  5. The thick-client half, if you have Instant Client: TNS_ADMIN=~/lab12154/b sqlplus fx_loader@FXRATES. Expected: ORA-12154, in the old or new wording depending on your client version. On a newer client the message names ~/lab12154/b/tnsnames.ora.

Then the one that surprised me. Add a sqlnet.ora next to the good file:

# ~/lab12154/a/sqlnet.ora
NAMES.DEFAULT_DOMAIN = example.com

Run SQL*Plus with TNS_ADMIN=~/lab12154/a and it fails: it now looks for FXRATES.example.com, which isn't in the file. Run the Python thin script with the same TNS_ADMIN and it gets past naming fine. Same directory, same alias, opposite results. Hold that thought for the mechanism.

Teardown is rm -rf ~/lab12154. Nothing else was touched.

Start here if you're in a hurry

Your connect string contained a name, not an address. The client tried to look that name up using the naming methods it's configured for (usually a tnsnames.ora file), and the lookup failed. Either it read a different file than you think, the alias isn't in that file in a form it can parse, or sqlnet.ora changed the rules of the lookup.

Which tnsnames.ora did it read?

Every ORA-12154 comes down to this, and the answer depends on which client is asking. That's why "works in SQL Developer, fails in SQL*Plus" is so common.

A full Oracle client or database home follows the order in the 26ai Net Services guide: the directory in TNS_ADMIN if it's set, then ORACLE_BASE_HOME/network/admin, then ORACLE_HOME/network/admin. Before it even opens the file, it checks NAMES.DIRECTORY_PATH in sqlnet.ora, which lists the naming methods to try in order. The default is local naming (tnsnames.ora), then LDAP, then Easy Connect. If someone's sqlnet.ora says NAMES.DIRECTORY_PATH = (LDAP), your perfect tnsnames.ora is never consulted. And here's the twist Oracle's own error page calls out: if EZCONNECT isn't in that list, even a host:port/service string can fail with ORA-12154.

Then NAMES.DEFAULT_DOMAIN. If it's set, an unqualified alias gets the domain appended before the lookup. That's my FXRATES turning into FXRATES.example.com in the lab.

Instant Client (and python-oracledb in thick mode, which uses it) looks in TNS_ADMIN and, by default, the network/admin directory inside the Instant Client install, per the python-oracledb initialization docs. The same page lists $HOME/.tnsnames.ora on Unix, which explains the "two files" variant of the newer message.

python-oracledb in thin mode is the odd one out. It reads exactly one file, tnsnames.ora, from config_dir if you pass it, otherwise from oracledb.defaults.config_dir, which it sets at startup from $TNS_ADMIN or $ORACLE_HOME/network/admin. It doesn't read sqlnet.ora at all. So NAMES.DEFAULT_DOMAIN and NAMES.DIRECTORY_PATH mean nothing to it. That's the whole explanation for the lab's split result, and it was the thing I had wrong: I'd assumed "Oracle client config" was one shared thing. It's per driver.

The JDBC thin driver is a separate implementation again. The JDBC Developer's Guide says to tell it where the file is, either with the oracle.net.tns_admin system property or with TNS_ADMIN in the URL (jdbc:oracle:thin:@FXRATES?TNS_ADMIN=/opt/fx/tns). I wouldn't rely on it picking up an environment variable your shell happened to export; the guide doesn't promise that, and a service started by systemd or a container won't have your shell's environment anyway.

SQL Developer has its own "Tnsnames Directory" setting under Tools, Preferences, Database, Advanced. When it works and SQLPlus doesn't, that preference is usually pointing at a directory SQLPlus has never heard of. On Windows with several Oracle homes installed, each home can carry its own TNS_ADMIN in the registry, and whichever sqlplus.exe is first on PATH decides which home you're in.

Fixing ORA-12154 client by client

Find the file first, change things second. The newer message gives you the path; on an older client, tnsping and the process's own TNS_ADMIN give you the rest.

What you found Fix Cost
Wrong directory Set TNS_ADMIN for that process, or config_dir (Python), or oracle.net.tns_admin / ?TNS_ADMIN= (JDBC) One more setting to carry between environments
Alias missing or misspelled Add it, starting in column 1 None
Syntax damage Fix parentheses, leading spaces, tabs or smart quotes pasted from a wiki None, once you spot it
NAMES.DEFAULT_DOMAIN in play Qualify the alias in the file, or remove the setting Touches every client sharing that sqlnet.ora
NAMES.DIRECTORY_PATH excludes what you use Add TNSNAMES or EZCONNECT back Same as above
You don't need an alias at all Use EZConnect Lose the central file, and features that need a full descriptor

The JDBC change, before and after:

# before: alias, and nobody told the driver where tnsnames.ora is
spring.datasource.url=jdbc:oracle:thin:@FXRATES

# after, option 1: point at the directory explicitly
spring.datasource.url=jdbc:oracle:thin:@FXRATES?TNS_ADMIN=/opt/fx/tns

# after, option 2: no alias at all
spring.datasource.url=jdbc:oracle:thin:@//db-fx:1521/FXPDB1

And Python:

conn = oracledb.connect(user="fx_loader", password=pw,
                        dsn="FXRATES", config_dir="/opt/fx/tns")  # explicit beats inherited

Not fixes: copying tnsnames.ora into every Oracle home until something works (five files drifting apart by next quarter), and putting @ before the alias in a login dialog. Oracle's guide calls that one out by name.

What I'd do instead

For applications, I don't ship aliases unless something needs a full descriptor (failover address lists, TLS, Autonomous Database wallets). An EZConnect string in the app's own config is one line and can't be silently redirected by a sqlnet.ora somebody edited on the host. If the SID-versus-service-name part of that string is fuzzy, here's the difference.

When you do need tnsnames.ora, give it one home per environment, commit it next to the app's other config, and point every client at it explicitly: TNS_ADMIN in the service unit or container env, config_dir in Python, ?TNS_ADMIN= in the JDBC URL. Explicit beats default every time here, because "default" means something different to each driver.

Keeping it from coming back

Have the service log its connect target and config directory at boot, and fail fast with that path in the message. That turns a mystery into one log line.

In CI, connect to a Testcontainers Oracle the same way production does. A test that passes with EZConnect proves nothing about production's alias.

Version notes worth keeping: 19c-era clients print the terse message; 23ai and 26ai clients print the alias and the file paths. python-oracledb thin mode never reads sqlnet.ora, and from python-oracledb 3.0, thick mode passes the DSN through to the Oracle client libraries by default (thick_mode_dsn_passthrough), so the thin-mode lookup rules don't apply there. Re-check all of that when you swap modes or upgrade.

On scope: I went through every wording of the message in Oracle's 19c and 26ai docs, plus the lookup order each client documents (full client, Instant Client, python-oracledb thin and thick, JDBC thin). The NAMES.DEFAULT_DOMAIN split between thin and thick was the part I didn't see coming.

If you remember nothing else

  • ORA-12154 is a client-side naming failure. The database never heard from you.
  • The question is always which tnsnames.ora this process read. Newer clients print the path; read it.
  • python-oracledb thin and JDBC thin do their own lookup with their own rules, and thin Python ignores sqlnet.ora completely.
  • An alias line with a leading space isn't an alias any more.
  • For app config, EZConnect removes the whole failure mode.

Why does SQL Developer connect but SQL*Plus gives ORA-12154?

They read different directories. SQL Developer uses its own Tnsnames Directory preference, while SQL*Plus follows TNS_ADMIN and then the Oracle home's network/admin. Point both at the same directory, or set TNS_ADMIN to the folder SQL Developer is using.

Where should tnsnames.ora be for Instant Client?

By default in the network/admin subdirectory of the Instant Client install, or anywhere you like if you set TNS_ADMIN to that directory. Don't set ORACLE_HOME for Instant Client; the python-oracledb docs say it shouldn't be set there.

Can I fix ORA-12154 without tnsnames.ora?

Yes, with EZConnect: user@//host:port/service_name. It skips local naming entirely, as long as EZCONNECT is allowed in NAMES.DIRECTORY_PATH (it is by default). For which name goes after the slash, see SID vs service name.

How do I set TNS_ADMIN for a Java application?

Either pass -Doracle.net.tns_admin=/path/to/dir to the JVM, or put it in the URL as jdbc:oracle:thin:@ALIAS?TNS_ADMIN=/path/to/dir. Both are in Oracle's JDBC Developer's Guide. If the driver then can't open a socket, that's a different problem: the Network Adapter error.

ora-12154tnsnamestns-adminsqlnet-oraoracle-clientsoracle-netoracle-database

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments