Sub topic 11
Connecting as SYSDBA and other privileged roles
This chapter assumes you can already connect as an ordinary user in SQL Developer and SQLcl, and that you know what a service name is. It covers the administrative roles, the two ways to authenticate with them, how to connect in each tool, and how to confirm the result.
What a privileged connection is
Most connections use a normal account such as HR, with only the privileges granted to that account. A privileged connection adds a system privilege to the session at login. System privileges like SYSDBA let you start up and shut down the instance, mount and open the database, and bypass the data dictionary checks that restrict normal users.
You request the privilege when you connect. It is not a different kind of account. The same username can connect with and without it.
| Privilege | Typical use | Session user shown |
|---|---|---|
| SYSDBA | Full administration, including startup, shutdown, recovery | SYS |
| SYSOPER | Startup, shutdown, backup, but no access to user data | PUBLIC |
| SYSBACKUP | Backup and recovery with RMAN | SYSBACKUP |
| SYSDG | Data Guard administration | SYSDG |
| SYSKM | Encryption key management | SYSKM |
A session with SYSDBA always works in the SYS schema, whatever username you typed. If you connect as hr as sysdba, objects you create land in SYS, not HR. This surprises many people.
Two ways to authenticate
Oracle accepts a privileged login in one of two ways.
- Operating system authentication. You are logged in to the database server as a member of the OSDBA group (usually
dbaon Linux). You connect with no password, using a local connection that bypasses the listener. - Password file authentication. The server keeps a password file, created with the
orapwdutility. Any privileged login over the network is checked against it. This requires theREMOTE_LOGIN_PASSWORDFILEparameter to beEXCLUSIVE(the default in current releases).
The diagram shows how the server decides which path applies.
Connecting from SQLcl
Add the role to the end of the connect string with AS.
Over the network
sql sys@//dbhost.example.com:1521/orclpdb1.example.com as sysdbaSQLcl prompts for the password, so it does not appear in your shell history. Avoid the sys/password@... form for the same reason.
You can also connect from inside a running SQLcl session:
SQL> connect sys@//dbhost.example.com:1521/orclpdb1.example.com as sysdba
Password? (**********?) ********
Connected.On the database server
If you are logged in to the server as an OS user in the dba group, set the environment so the client can find the instance, then use the slash form:
export ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1
export ORACLE_SID=orcl
sql / as sysdbaThis path does not use the listener, so it works even when the listener is down or the database is only mounted. That makes it the standard way to recover a broken instance.
Connecting from SQL Developer
In the New / Select Database Connection dialog, on the Basic tab, fill in the username (for example sys), the password, hostname, port, and service name as for any connection. Then change the Role drop-down from default to SYSDBA (or SYSOPER, SYSBACKUP, and so on).
Points to check:
- Use the service name option, not SID, unless you know the database registers only an SID.
- Save the connection under a name that shows the role, such as
orclpdb1_sys_sysdba. A connection named justorclpdb1invites mistakes later. - Choose the Test button before Connect. Test reports
Status: Successor the Oracle error. - If you use a TNS alias, select the TNS connection type, choose the network alias, and set the Role the same way.
SQL Developer has no slash form, because it always connects over the network. For a local OS-authenticated login, use SQLcl.
Which database you land in
With a multitenant database, the service name you use decides the scope of SYSDBA.
- A service for the root container (often the CDB's own service) gives you SYSDBA across the whole container database. You can start and stop the whole instance.
- A service for a pluggable database (PDB) gives you SYSDBA inside that PDB only. You can manage that PDB's contents, but not the instance.
Check which one you have after connecting:
SQL> show con_name
CON_NAME
------------------------------
ORCLPDB1
SQL> show user
USER is "SYS"CDB$ROOT means you are in the root. Anything else is a PDB.
Verifying a privileged session
Extend the verification query from earlier in this tutorial with the ISDBA attribute, which is TRUE only when the session was opened with SYSDBA:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'ISDBA') as is_dba,
sys_context('USERENV', 'CON_NAME') as container,
sys_context('USERENV', 'SERVICE_NAME') as service_name
from dual;For a SYSDBA session you should see SYS, TRUE, and the container you expect. If is_dba is FALSE, you connected without the role. Check for a typo or for the SQL Developer Role drop-down still on default.
To see who is allowed to use privileged roles over the network, query the password file view as a privileged user:
select username, sysdba, sysoper, sysbackup
from v$pwfile_users
order by username;Granting a privilege to another user
Only a user who already has the privilege can grant it. Connected as SYS:
grant sysbackup to backup_admin;The grant adds the user to the password file. The user then connects with connect backup_admin@... as sysbackup. Revoke with revoke sysbackup from backup_admin;.
Prefer the narrow roles. A backup operator needs SYSBACKUP, not SYSDBA. This limits the damage from a stolen password and makes audit records easier to read, because activity is recorded against the specific role.
Common errors
| Error | Likely cause | What to do |
|---|---|---|
| ORA-01017: invalid username/password | Wrong password, or the account is not in the password file | Retype the password. For SYS, check that the password file matches the one you expect. |
| ORA-01031: insufficient privileges | The user lacks the requested privilege, or on `/ as sysdba` the OS user is not in the OSDBA group | Grant the privilege from SYS, or run as an OS user in `dba`. |
| ORA-12162 or ORA-12547 on local login | `ORACLE_HOME` or `ORACLE_SID` is missing or wrong | Set both variables in the same shell before starting SQLcl. |
| Connected to an idle instance | The SID is right but the instance is not started | Run `startup` to start it, if that is what you intend. |
| ORA-28009: connection as SYS should be as SYSDBA or SYSOPER | You connected as SYS with no role | Add `as sysdba`. |
Cloud and managed databases
Oracle Autonomous Database does not give you SYSDBA. The administrator account is ADMIN, and you connect with the wallet as shown in the cloud chapter. Other managed services, such as Amazon RDS for Oracle, also withhold SYSDBA and provide a master user with a limited set of privileges. If as sysdba fails with ORA-01031 on a managed service, that is by design, not a configuration mistake.
Saved connections and credentials
SQL Developer stores the Role with a saved connection, so a SYSDBA entry reconnects with the role every time. Be careful about saving a password for a SYS connection, for the reasons covered in the credentials chapter. If you must keep one, restrict who can read your connection store and prefer prompting for the password.
Exercise
You are logged in as the oracle OS user, which is in the dba group, on the database server. The listener is stopped. Which SQLcl command lets you connect as SYSDBA, and why does it work without the listener?
Show answer
Set ORACLE_HOME and ORACLE_SID, then run sql / as sysdba. The slash form is a local (bequeath) connection with operating system authentication. It starts a server process directly for the instance on the same machine and checks the OS group membership, so it needs neither the listener nor a password.
Exercise
You connect with connect hr@//dbhost.example.com:1521/orclpdb1.example.com as sysdba, then create a table called notes. In which schema does the table end up, and how can you confirm the session was opened with the role?
Show answer
The table is created in the SYS schema, because a SYSDBA session always works as SYS regardless of the username given. Confirm the role with select sys_context('USERENV','ISDBA') from dual;, which returns TRUE, and select sys_context('USERENV','SESSION_USER') from dual;, which returns SYS.