Sub topic 7
Creating a development user instead of working as SYS or ADMIN
This chapter assumes you can already reach a database in at least one of the three environments from earlier chapters. You can use the 26ai Free container, an Always Free Autonomous Database, or FreeSQL. You should know basic CREATE TABLE and INSERT syntax and be able to run a script in SQL*Plus, SQLcl, or Database Actions.
Why not work as SYS or ADMIN?
Every Oracle practice database ships with an administrative account. In the 26ai Free container the main one is SYS. In Autonomous Database it is ADMIN. These accounts are meant for managing the database, not for building application objects.
Working in an administrative account causes four problems:
- Wrong habits. Your practice code runs with far more power than real application code ever will. Scripts that work as
SYScan fail for an ordinary user because of a missing privilege. You find out only when you move the code somewhere real. - Risk to the database. A mistaken
DROPorDELETErun as an administrator can damage data dictionary objects or other schemas. Oracle explicitly advises against creating your own objects in theSYSschema. - Cluttered schema. Objects you create belong to the schema of the user you connect as. Tables in
ADMINmix with objects the platform created for itself. - Hard cleanup. If everything lives in one dedicated schema, you can reset your practice area with one
DROP USER ... CASCADEcommand. That is much harder when your objects sit in an administrative schema.
The fix is a dedicated, non-administrative user. In Oracle, a user and its schema are the same thing: creating the user app_user creates an empty schema called APP_USER that owns whatever tables you build.
What a development user needs
A new user can do nothing at first. It cannot even log in. Three things make it useful:
- A password. This is set with
IDENTIFIED BY. - Privileges.
CREATE SESSIONallows login.CREATE TABLE,CREATE VIEW,CREATE SEQUENCE,CREATE PROCEDURE, and similar privileges allow building objects. - A tablespace quota. A tablespace is the storage area where table data lives. A user who has
CREATE TABLEbut no quota gets an error on the first insert.
Instead of granting each privilege by hand, you can grant a role. A role is a named bundle of privileges. Oracle provides DB_DEVELOPER_ROLE for this purpose. It covers login, the usual CREATE privileges for schema objects, and other developer features. You can read its exact contents in your own database. The check is at the end of this chapter's SQL examples.
Option 1: 26ai Free container
Make sure the container is running and the log shows DATABASE IS READY TO USE!. Then connect as SYS to the pluggable database FREEPDB1:
sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaThe service name in the connection string decides where your user is created. The FREEPDB1 service puts you in the pluggable database. Confirm that before you create anything:
SHOW CON_NAMEThe output should say FREEPDB1. If it says CDB$ROOT, you are in the container root, which is the wrong place for application users.
Now create the user:
CREATE USER app_user IDENTIFIED BY "ChangeMe_123" QUOTA UNLIMITED ON users;
GRANT DB_DEVELOPER_ROLE TO app_user;The statements do the following:
IDENTIFIED BYsets the password. The double quotes protect the password from being read as an identifier. They also let you use characters such as#or@.QUOTA UNLIMITED ON usersallows the user to store data in theUSERStablespace. This is fine for practice. In a shared database you would set a size such asQUOTA 500M ON users.DB_DEVELOPER_ROLEsupplies the privileges.
If you want to be explicit about the target container instead of relying on the service name, switch to it first:
ALTER SESSION SET CONTAINER = FREEPDB1;Now leave the administrative session and log in as the new user:
sqlplus app_user/ChangeMe_123@localhost:1521/FREEPDB1The practice password reuses the one from the container setup only to keep the examples short. For anything you keep, use a different password for each account.
Why not create the user in the CDB?
The container database (CDB) hosts the pluggable databases and has its own system-level users. If you connect to the CDB root and run CREATE USER app_user ..., Oracle rejects it with ORA-65096: invalid common user or role name. In the root, new users must be common users whose names start with C##. A common user exists in every container. You rarely want that for application work. A local user created inside FREEPDB1 exists only there, which matches how real applications are deployed.
Option 2: Autonomous Database
In Autonomous Database you do not have SYS. The administrative account is ADMIN, and you set its password when you created the database. Open Database Actions, sign in as ADMIN, and choose SQL. Then run:
CREATE USER app_user IDENTIFIED BY "ChangeMe_12345" QUOTA UNLIMITED ON data;
GRANT DB_DEVELOPER_ROLE TO app_user;Two differences from the container:
- The tablespace is
DATA. Autonomous Database uses a managed tablespace with that name. You do not choose or size tablespaces yourself. - Passwords must meet a policy. Passwords must be 12 to 30 characters, with at least one uppercase letter, one lowercase letter, and one number. They cannot contain the username or a double quote character. A weak password fails with an
ORA-28003error.
A user created with SQL can connect by SQLNet or a driver, but it cannot sign in to the Database Actions web interface until its schema is enabled for web access. You can switch this on in Database Actions under Database Users*, or with SQL:
BEGIN
ORDS_ADMIN.ENABLE_SCHEMA(
p_enabled => TRUE,
p_schema => 'APP_USER',
p_url_mapping_type => 'BASE_PATH',
p_url_mapping_pattern => 'app_user',
p_auto_rest_auth => TRUE
);
COMMIT;
END;
/After that, Database Actions gives you a sign-in URL for the new user. Alternatively, create the user from the Database Users page. That page offers a Web Access switch in the same dialog.
Option 3: FreeSQL
FreeSQL gives you a ready-made schema when you sign in. You cannot create database users there, and you do not need to. Treat the FreeSQL schema as your development user and skip this chapter's CREATE USER steps. The verification query later in the chapter works in FreeSQL as well.
| Item | 26ai Free container | Autonomous Database | FreeSQL |
|---|---|---|---|
| Administrative account | `SYS`, `SYSTEM`, `PDBADMIN` | `ADMIN` | Not available |
| Where you create the user | `FREEPDB1` | The database, through Database Actions | Not needed |
| Tablespace for quota | `USERS` | `DATA` | Managed for you |
| Password rules | Basic | 12 to 30 characters with mixed case and a digit | Managed by sign-in |
| Extra step | None | Enable web access to use Database Actions as this user | None |
Checking what the role contains
DB_DEVELOPER_ROLE can change between releases, so check it instead of trusting a list from memory. Sign in as an administrator and query the data dictionary:
SELECT privilege
FROM dba_sys_privs
WHERE grantee = 'DB_DEVELOPER_ROLE'
ORDER BY privilege;To see what a particular user has, switch to that user and use the session views:
SELECT * FROM session_privs ORDER BY privilege;
SELECT * FROM session_roles ORDER BY role;session_privs lists privileges active in the current session, including those from roles. A privilege granted through a role does not work inside a stored procedure unless it was granted directly. If a CREATE PROCEDURE body fails with ORA-01031: insufficient privileges on something that works in a plain script, this is the likely cause.
Verify with the user you just created
Connect as app_user and run the check from the spine. It reports your container and your session user:
SELECT sys_context('USERENV', 'CON_NAME') AS container_name,
sys_context('USERENV', 'SESSION_USER') AS session_user
FROM dual;In the container, expect FREEPDB1 and APP_USER. In Autonomous Database, the container name is a generated name for your database, and the user should be APP_USER. If the user shows SYS or ADMIN, you are still in the administrative session.
Then run the create, insert, and query cycle:
CREATE TABLE practice_note (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
body VARCHAR2(200) NOT NULL
);
INSERT INTO practice_note (body) VALUES ('Hello from my practice database');
COMMIT;
SELECT id, body FROM practice_note;A successful run proves that CREATE TABLE works, that the quota is in place, and that you can read and write your own data.
Common errors
| Error | Likely cause | Fix |
|---|---|---|
| `ORA-65096: invalid common user or role name` | You ran `CREATE USER` in `CDB$ROOT` | Reconnect to `FREEPDB1`, or run `ALTER SESSION SET CONTAINER = FREEPDB1` |
| `ORA-01045: user APP_USER lacks CREATE SESSION privilege` | The grant did not run, or ran for another user | Grant `DB_DEVELOPER_ROLE` again as an administrator |
| `ORA-01950: no privileges on tablespace 'USERS'` | The user has no quota | `ALTER USER app_user QUOTA UNLIMITED ON users` |
| `ORA-01920: user name 'APP_USER' conflicts with another user or role name` | The user already exists | Use `ALTER USER` to change it, or drop and recreate it |
| `ORA-28003: password verification for the specified password failed` | The password breaks the policy (Autonomous) | Choose a longer password with mixed case and a digit |
| `ORA-01017: invalid username/password` | Wrong password, or a connection to the wrong service | Check the password and the service name in the connect string |
For quota problems in Autonomous Database, replace users with data in the fix.
Resetting your practice area
Because everything lives in one schema, you can wipe it cleanly:
DROP USER app_user CASCADE;CASCADE removes the user's tables, views, and all other objects with it. Run this as an administrator, and only when you mean it, because the data cannot be recovered. A logged-in session of that user blocks the drop with ORA-01940, so disconnect first.
Exercise
You connect to the 26ai Free container as SYS using the service name FREEPDB1. You run CREATE USER app_user IDENTIFIED BY "ChangeMe_123" QUOTA UNLIMITED ON users; and then GRANT DB_DEVELOPER_ROLE TO app_user;. You log in as appuser and create a table, and the CREATE TABLE succeeds. The next statement inserts a row, and the whole script ends with a COMMIT. Which single query run as appuser confirms both your container and your user?
Show answer
Run SELECT sys_context('USERENV', 'CON_NAME') AS container_name, sys_context('USERENV', 'SESSION_USER') AS session_user FROM dual;. It should return FREEPDB1 and APP_USER.
Exercise
A colleague connects to the container with a service name that lands in CDB$ROOT and runs CREATE USER app_user IDENTIFIED BY "ChangeMe_123";. What error do they see, why does it happen, and what are two ways to fix it?
Show answer
They see ORA-65096: invalid common user or role name. In the root container, users must be common users whose names start with C##. Fix it by reconnecting with the FREEPDB1 service name, or by running ALTER SESSION SET CONTAINER = FREEPDB1 before the CREATE USER statement.
Exercise
In Autonomous Database, app_user can run SQL from a driver but cannot sign in to Database Actions. What is missing, and how can you add it?
Show answer
The schema is not enabled for web access. Turn on Web Access for the user on the Database Users page in Database Actions, or run ORDSADMIN.ENABLESCHEMA as ADMIN with penabled => TRUE and pschema => 'APP_USER'.