Sub topic 6
Using a TNS Alias and `tnsnames.ora` in SQL Developer
This chapter assumes you know basic SQL, can use a file system and terminal, and have access to an Oracle Database account. SQL Developer must be installed. You also need a tnsnames.ora file supplied by your database administrator or created from the database connection details.
A TNS alias is a short name that represents an Oracle network connection. The alias is stored in tnsnames.ora, a text configuration file. SQL Developer reads the alias and uses its connection details, such as the host, port, and service name.
How the alias works
A TNS alias keeps connection details out of the SQL Developer connection form. For example, the alias ORCLPDB can represent this descriptor:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)The HOST identifies the database server. The PORT identifies the listener port. The SERVICE_NAME identifies the database service that accepts the session.
Tell SQL Developer where `tnsnames.ora` is
SQL Developer must know which directory contains the file.
- Open SQL Developer.
- Select Tools > Preferences.
- Select Database > Advanced.
- Set Tnsnames Directory to the directory containing
tnsnames.ora. - Apply the setting and reopen the connection dialog.
The directory might be an Oracle client network directory, such as $ORACLE_HOME/network/admin on Linux or an Oracle installation directory on Windows. The exact location depends on how Oracle client software was installed.
Some environments use the TNS_ADMIN environment variable to identify this directory. If SQL Developer does not show the expected aliases, check both the configured directory and the value of TNS_ADMIN.
Create and test the connection
In SQL Developer, select New Connection and enter:
| Field | Value |
|---|---|
| Username | Your Oracle username, such as `HR` |
| Password | Your database password |
| Connection Type | TNS |
| Network Alias | `ORCLPDB` |
| Role | Default, unless an administrator gave you another role |
Select Test. A successful test reports Status: Success. Select Connect to open the worksheet.
After connecting, verify the session instead of relying only on the connection name:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;The result confirms which user, service, and server host received your session.
Troubleshoot alias connections
ORA-12154usually means SQL Developer cannot resolve the alias. Check the spelling, file location, andTnsnames Directorysetting.ORA-12541usually means no listener is reachable at the host and port in the descriptor.ORA-12514means the listener is reachable but does not know the requested service name.- A successful alias lookup does not prove that the username and password are valid. Invalid credentials can still produce
ORA-01017.
Exercise
SQL Developer lists no aliases after you choose the TNS connection type. What should you check first?
Show answer
Check that the Tnsnames Directory points to the directory containing tnsnames.ora, then verify that the alias is spelled correctly and reopen the connection dialog.