Sub topic 4
Connecting to the CDB and the FREEPDB1 Pluggable Database
This chapter assumes that you know basic SQL, can use a terminal, and have started Oracle Database Free in a local container. It also assumes that the container publishes port 1521 and that an Oracle SQL client, such as SQL*Plus, is available.
Oracle Database Free uses a multitenant architecture. A container database (CDB) is the main database instance. It contains one or more pluggable databases (PDBs). A PDB is an isolated database environment that stores application objects such as tables, users, and data.
For practice work, FREEPDB1 is usually the correct place to connect. The root container, named CDB$ROOT, is mainly for administration.
Understand the connection targets
A client does not connect only to a host and port. It also requests a database service. In an Easy Connect string, the format is:
host:port/service_nameFor a local Oracle Free container, the common values are:
| Value | Meaning |
|---|---|
| `localhost` | The database is running on your computer |
| `1521` | The Oracle Net listener port |
| `FREE` | The service for the container database |
| `FREEPDB1` | The service for the practice pluggable database |
The service name determines your initial container. Connecting to FREEPDB1 places your session directly in that PDB.
Connect directly to FREEPDB1
The following command connects as SYS with administrative privileges:
sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaReplace ChangeMe_123 with the password assigned when the container was created. The as sysdba clause is required for a SYS connection.
After connecting, check the current container and user:
SELECT sys_context('USERENV', 'CON_NAME') AS container_name,
sys_context('USERENV', 'SESSION_USER') AS session_user
FROM dual;Expected output includes:
CONTAINER_NAME SESSION_USER
--------------- ------------
FREEPDB1 SYSYou can also use SQL*Plus commands:
SHOW CON_NAME;
SHOW USER;A session connected to FREEPDB1 can create tables and application users in that PDB. Those objects do not appear in another PDB unless you explicitly create them there.
Connect to the CDB root
To inspect the root container, connect through the FREE service:
sqlplus sys/ChangeMe_123@localhost:1521/FREE as sysdbaThen run:
SHOW CON_NAME;
SELECT name, open_mode, con_id
FROM v$containers
ORDER BY con_id;The result normally includes CDB$ROOT and FREEPDB1. READ WRITE means that the container is open for changes.
From a SYSDBA session in the root, you can switch containers:
ALTER SESSION SET CONTAINER = FREEPDB1;
SHOW CON_NAME;This changes the current session only. It does not move the database or change where other users are connected.
You can switch back:
ALTER SESSION SET CONTAINER = CDB$ROOT;A connection made directly to FREEPDB1 is usually safer for development because it avoids accidentally running application statements in the root.
Understand common connection errors
The error message often identifies which layer failed.
| Symptom | Likely cause | Check |
|---|---|---|
| Connection refused | The container or listener is not running | `docker ps` and container logs |
| ORA-12514 | The requested service is unknown | Check the service name and listener status |
| ORA-01017 | The username or password is invalid | Check the password and spelling |
| ORA-65096 | A local user was created in the root | Switch to `FREEPDB1` first |
| ORA-65040 | A command is not allowed in the current container | Run it in the root or target PDB as appropriate |
First check whether the database finished starting:
docker logs -f oracle-free
# Wait for the line: DATABASE IS READY TO USE!If the container is running but the connection still fails, verify that port 1521 is published:
docker psThe ports column should show a mapping similar to 0.0.0.0:1521->1521/tcp.
Exercise
You connected to FREE and SHOW CON_NAME reports CDB$ROOT. Which statement switches the current session to the practice PDB?
Show answer
ALTER SESSION SET CONTAINER = FREEPDB1;