Sub topic 9
Verifying who and where you are connected
This chapter assumes you can already open a connection in SQL Developer or SQLcl and run a basic query. It covers how to confirm that a session went to the database, service, and user you intended. It also covers the places where the answer can mislead you.
Why verify at all
A successful login only proves that some database accepted some credentials. It does not prove that you reached the right one. Several common situations produce a working but wrong session:
- A saved connection in SQL Developer still points at a test database, and you think it is production, or the reverse.
- A TNS alias in
tnsnames.oraresolves to a different host than a colleague's copy of the same file. - You connected as a user that has the right name but sits in a different pluggable database (PDB).
- A load balancer or SCAN address sent you to a different instance than the one you expected.
Checking takes a few seconds. Do it before you run anything that changes data, and always before DDL or scripts that delete rows.
What a session knows about itself
Oracle keeps session facts in an application context named USERENV. You read it with the SYS_CONTEXT function. This is the first query to run after connecting:
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;Each column answers a different question:
| Column | Question it answers | Example value |
|---|---|---|
| `SESSION_USER` | Which database account am I logged in as? | `HR` |
| `SERVICE_NAME` | Which service did the listener route me to? | `orclpdb1.example.com` |
| `SERVER_HOST` | Which machine is running the database instance? | `dbhost` |
SERVER_HOST returns the host name of the database server as the server itself reports it. This can differ from the name you typed. If you connected through a DNS alias, a VIP, or a container port mapping, the value shows the real machine or container name.
More useful USERENV attributes
The three attributes above are a starting point. These others are often more decisive:
| Attribute | What it tells you |
|---|---|
| `CON_NAME` | The container you are in: `CDB$ROOT` or a PDB name |
| `DB_NAME` | The database name (the CDB name on a multitenant system) |
| `INSTANCE_NAME` | The instance serving you, which matters on RAC |
| `CURRENT_SCHEMA` | The schema used to resolve unqualified object names |
| `AUTHENTICATED_IDENTITY` | The identity used for authentication |
| `IP_ADDRESS` | The client IP address as the server sees it |
A single query can collect them:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'CURRENT_SCHEMA') as current_schema,
sys_context('USERENV', 'CON_NAME') as con_name,
sys_context('USERENV', 'DB_NAME') as db_name,
sys_context('USERENV', 'INSTANCE_NAME') as instance_name,
sys_context('USERENV', 'SERVICE_NAME') as service_name
from dual;CON_NAME is the attribute people forget. On a multitenant database, the same user name can exist in several PDBs. If CON_NAME is CDB$ROOT when you expected ORCLPDB1, you are in the wrong container, even though the login worked.
Checking from SQLcl without a query
SQLcl can show the same facts without you writing SQL. After a connection, these commands are available:
SQL> show user
USER is "HR"
SQL> show con_name
CON_NAME
------------------------------
ORCLPDB1show user reports the session user. show con_name reports the container. The SHOW commands are SQL*Plus-compatible, so they are handy in scripts you also run elsewhere.
SQLcl also has the connmgr command. connmgr list shows saved connections, but it describes what is stored, not what the current session is using. Treat the USERENV query as the source of truth for the live session.
Checking in SQL Developer
In SQL Developer, the connection name in the Connections panel is only a label you chose. It is not evidence of where you are connected. To verify:
- Open a SQL Worksheet on the connection.
- Paste the
USERENVquery and run it with Run Statement (Ctrl+Enter). - Compare the result with what you expect.
You can also hover over the worksheet tab or the connection node, which shows the user and the connect details. Use that as a hint, and use the query as the check.
A good habit in SQL Developer is to give connections names that carry the environment, such as hr_DEV and hr_PROD. You can also assign a color to a connection in its properties, so a production worksheet is visibly different.
Identity versus schema
SESSION_USER and CURRENT_SCHEMA can differ. After this statement, you are still the same user, but unqualified names resolve in another schema:
alter session set current_schema = sales;
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'CURRENT_SCHEMA') as current_schema
from dual;The result shows HR for SESSION_USER and SALES for CURRENT_SCHEMA. A statement such as delete from orders now affects SALES.ORDERS, even though your login is HR. When you ask "who am I?", check both.
Proxy users
With proxy authentication, you log in as one account and act as another, written as proxy_user[target_user]. In that case SESSION_USER is the target user, and PROXY_USER holds the account that authenticated. If these look unfamiliar in your environment, query both:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'PROXY_USER') as proxy_user
from dual;PROXY_USER is NULL when no proxy is in play.
A repeatable verification flow
The following diagram shows the order of checks. Each step catches a different kind of mistake.
Making the check automatic in SQLcl
If you connect often, put the query in a file and run it each time. Create whoami.sql:
set pagesize 0 feedback off
select 'user=' || sys_context('USERENV', 'SESSION_USER')
|| ' schema=' || sys_context('USERENV', 'CURRENT_SCHEMA')
|| ' container='|| sys_context('USERENV', 'CON_NAME')
|| ' service=' || sys_context('USERENV', 'SERVICE_NAME')
|| ' host=' || sys_context('USERENV', 'SERVER_HOST')
from dual;
set feedback on pagesize 14Then run it after any connect:
SQL> connect -name orclpdb_hr
Connected.
SQL> @whoami.sql
user=HR schema=HR container=ORCLPDB1 service=orclpdb1.example.com host=dbhostSQLcl also runs a login.sql file at startup if it finds one in the current directory or a configured path. You can place the USERENV query there. Be aware that login.sql runs at startup, and in some versions it also runs again after each connect, so confirm how your version behaves before relying on it. A login.sql that runs before any connection exists will fail on the query, so test it.
Common mistakes
- Trusting the connection name. The label in SQL Developer or
connmgris a string. Verify with a query. - Checking only the user. A correct
SESSION_USERin the wrong PDB is still wrong. Always checkCON_NAMEorSERVICE_NAME. - Reading
SERVER_HOSTas the name you typed. It is what the server reports, so a mismatch is not always a mistake. In containers it is often a container ID. - Forgetting
CURRENT_SCHEMA. Afteralter session set current_schema, unqualified names point elsewhere. - Assuming a reconnect changes nothing. On RAC or behind a load balancer,
INSTANCE_NAMEmay differ from one connection to the next. That is normal, but it matters when you compare session-level diagnostics.
Summary of the checks
| Goal | SQL | SQLcl command |
|---|---|---|
| Confirm the account | `SESSION_USER` | `show user` |
| Confirm the container | `CON_NAME` | `show con_name` |
| Confirm the service | `SERVICE_NAME` | none |
| Confirm the server | `SERVER_HOST`, `INSTANCE_NAME` | none |
| Confirm name resolution | `CURRENT_SCHEMA` | none |
Exercise
You connect as HR to a multitenant database and run the USERENV query. SESSIONUSER is HR, but CONNAME is CDB$ROOT, and you expected the pluggable database ORCLPDB1. What most likely went wrong, and what should you check in your connection settings?
Show answer
The connection used a service name that routes to the root container (for example the CDB service, or a SID-style connection) instead of the PDB's service. Check the service name in the Easy Connect string, TNS alias, or SQL Developer connection (it should be the PDB service such as orclpdb1.example.com), then reconnect and run the query again to confirm CON_NAME is ORCLPDB1.
Exercise
After running alter session set current_schema = sales; as user HR, what do SESSIONUSER and CURRENTSCHEMA return, and why does it matter for delete from orders?
Show answer
SESSIONUSER returns HR and CURRENTSCHEMA returns SALES. The unqualified name ORDERS resolves in the current schema, so the statement deletes from SALES.ORDERS, not HR.ORDERS, subject to the privileges HR has on that table.