Sub topic 4
Connecting to the CDB and the FREEPDB1 pluggable database
This chapter assumes you can run a container with Docker or Podman, that you have the oracle-free container from the previous chapter running, and that you know basic SQL. You do not need to know Oracle administration. The chapter explains the one architectural idea you need: one Oracle instance can host several databases, and you must choose which one to connect to.
Why there are two places to connect
Oracle Database 26ai Free uses the multitenant architecture. A single running instance holds one container database (CDB). Inside it live several pluggable databases (PDBs). A PDB looks and behaves like a complete database with its own users, tables, and tablespaces. The CDB provides the shared memory, processes, and redo logs.
Your container has three containers in this sense:
| Container | CON_ID | Purpose |
|---|---|---|
| `CDB$ROOT` | 1 | The root. Holds Oracle-supplied metadata and common users. You administer the instance from here. |
| `PDB$SEED` | 2 | A read-only template that Oracle copies when it creates a new PDB. You never connect to it for practice. |
| `FREEPDB1` | 3 | The pluggable database created for you. This is where your practice tables and users belong. |
Each container is reachable through a service name. A service name is a label that the listener uses to route a connection. The listener is the network process that accepts connections on port 1521 and hands them to the database. For this image the two service names that matter are:
FREEroutes toCDB$ROOT.FREEPDB1routes to the pluggable databaseFREEPDB1.
The service name you put in the connection string decides which container your session lands in. Passwords and ports are the same for both.
The Easy Connect string
The simplest way to name a database is Easy Connect. It has this shape:
host:port/service_nameFor the container from the previous chapter, published on your machine at port 1521, the two strings are:
localhost:1521/FREEfor the root.localhost:1521/FREEPDB1for the pluggable database.
The / before the service name matters. If you write localhost:1521:FREE, you are using an older syntax that treats the last part as a SID (system identifier of an instance), not a service name. Pluggable databases have no SID, so that form cannot reach FREEPDB1.
Waiting until the database is ready
On first start the container creates the database, which takes several minutes. Connecting earlier gives errors that look like configuration problems but are only timing. Follow the log until the ready line appears:
docker logs -f oracle-free
# Wait for the line: DATABASE IS READY TO USE!Press Ctrl+C to stop following the log. This does not stop the container.
Connecting to FREEPDB1 as an administrator
The ORACLE_PWD value you passed to docker run set the password for three accounts: SYS, SYSTEM, and PDBADMIN. Use SYS with the AS SYSDBA clause when you need full administrative control:
sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaThis command needs SQLPlus on your host. If you do not have it, run SQLPlus inside the container, where it is already installed:
docker exec -it oracle-free sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaThe localhost here refers to the inside of the container. The listener runs there as well, so the string works unchanged.
Once connected, confirm where you are:
SHOW CON_NAME
SHOW USERSHOW CON_NAME should print FREEPDB1 and SHOW USER should print USER is "SYS". These are SQL*Plus commands, so they take no semicolon. The equivalent SQL query, which works in any client, is:
SELECT sys_context('USERENV', 'CON_NAME') AS container_name,
sys_context('USERENV', 'SESSION_USER') AS session_user
FROM dual;Whenever a statement behaves unexpectedly, run this query first. A large share of "my table has disappeared" problems come from a session in the wrong container.
Connecting to the CDB root
To look at the whole instance, use the FREE service:
docker exec -it oracle-free sqlplus sys/ChangeMe_123@localhost:1521/FREE as sysdbaHere SHOW CON_NAME prints CDB$ROOT. From the root you can list the PDBs and their state:
SELECT con_id, name, open_mode
FROM v$pdbs
ORDER BY con_id;You should see PDB$SEED with READ ONLY and FREEPDB1 with READ WRITE. A PDB must be open in READ WRITE mode before ordinary users can create tables in it. The Free image opens FREEPDB1 automatically on every start, so you normally do not manage this.
A second shortcut exists when you are already inside the container. Operating system authentication lets the oracle OS user connect without a password and always lands in the root:
docker exec -it oracle-free sqlplus / as sysdbaThis is convenient for administration, but it connects to CDB$ROOT, not to FREEPDB1. If you then create a table, it goes to the wrong place. Switch containers as shown in the next section.
Switching containers within a session
If you are connected to the root as a privileged common user, you can move into a PDB without reconnecting:
ALTER SESSION SET CONTAINER = FREEPDB1;
SHOW CON_NAMETo go back:
ALTER SESSION SET CONTAINER = CDB$ROOT;This works only for users with the SET CONTAINER privilege, such as SYS and SYSTEM. Ordinary development users cannot do it, and should not need to. They connect to FREEPDB1 directly through its service name.
Common users and local users
Oracle distinguishes two kinds of user accounts, and the difference explains most of the confusion around CREATE USER in a multitenant database.
- A common user exists in every container. Its name must start with
C##when you create it, unless Oracle itself created it.SYSandSYSTEMare common users supplied by Oracle. - A local user exists in exactly one PDB. Its name has no prefix.
PDBADMINis a local administrator ofFREEPDB1.
If you run CREATE USER app_user ... while connected to CDB$ROOT, Oracle rejects it with ORA-65096: invalid common user or role name. The error does not mean the name is bad. It means you are in the root, where only C## names are allowed. The fix is to connect to FREEPDB1 and create the user there. The next chapter on development users covers the exact statements. For now, remember that your practice objects belong in FREEPDB1 as local users.
You can check which kind a user is:
SELECT username, common
FROM dba_users
WHERE username IN ('SYS', 'SYSTEM', 'PDBADMIN')
ORDER BY username;Run in FREEPDB1, SYS and SYSTEM show YES and PDBADMIN shows NO.
Connecting with SQLcl
SQLcl is Oracle's command-line client. It is a single Java-based tool that needs no Oracle client installation and supports history and formatting. The connection string is the same as for SQL*Plus:
sql sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdbaAny tool that accepts a host, port, and service name works the same way. In a graphical client such as SQL Developer or in a driver configuration, enter host localhost, port 1521, and service name FREEPDB1. Choose "Service name" and not "SID" if the tool asks.
Using a named alias with tnsnames.ora
Typing the full string each time gets tedious. Oracle clients read a file called tnsnames.ora that maps short aliases to connection descriptors. Create one in a directory of your choice:
FREEPDB1_LOCAL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = FREEPDB1))
)Tell the client where the file is with the TNS_ADMIN environment variable, then connect using the alias:
export TNS_ADMIN="$HOME/oracle-net"
sqlplus sys@FREEPDB1_LOCAL as sysdbaSQL*Plus prompts for the password, which also keeps it out of your shell history. Autonomous Database uses the same mechanism with a downloaded wallet, which a later chapter covers.
Checking the listener
If a connection fails, ask the listener what it knows. Run this on the host:
docker exec -it oracle-free lsnrctl statusThe output lists the services the listener has registered. Look for FREE and FREEPDB1. If FREEPDB1 is missing, the PDB is not open or the database is still starting. If the command reports that it cannot connect, the listener is not running.
What the common errors mean
| Error | Likely cause | First check |
|---|---|---|
| `ORA-12541: TNS:no listener` | Container not running, port not published, or wrong port | `docker ps` and the `-p 1521:1521` flag |
| `ORA-12514: listener does not currently know of service requested` | Database still starting, or the service name is wrong | `docker logs oracle-free` and `lsnrctl status` |
| `ORA-01017: invalid username/password` | Wrong password, or the user exists in a different container | Confirm `ORACLE_PWD` and the service name |
| `ORA-65096: invalid common user or role name` | `CREATE USER` run in `CDB$ROOT` | `SHOW CON_NAME`, then reconnect to `FREEPDB1` |
| `ORA-01031: insufficient privileges` | `SYS` connection without `AS SYSDBA` | Add `as sysdba` |
A full troubleshooting chapter comes later. This table is enough to recognise the errors that this chapter's steps can produce.
A quick end-to-end check
Run these steps in order. Together they confirm the listener, the service name, and the container.
- Confirm the container is up:
docker ps --filter name=oracle-free. - Confirm the ready line:
docker logs oracle-free | grep "DATABASE IS READY TO USE". - Connect to
FREEPDB1asSYSand run thesys_contextquery.container_namemust readFREEPDB1. - Connect to
FREEand run thev$pdbsquery.FREEPDB1must showREAD WRITE.
If all four pass, the connection layer is working. Anything that fails afterwards is a user, privilege, or SQL problem, not a networking one.
Exercise
You connect with sqlplus sys/ChangeMe_123@localhost:1521/FREE as sysdba and run CREATE USER app_user IDENTIFIED BY "ChangeMe_123";. Oracle returns ORA-65096. Explain why, and give the connection string that lets you create the user as a local user.
Show answer
The service name FREE routes the session to CDB$ROOT, where user names must start with C## because users created there are common users. app_user has no prefix, so Oracle rejects it. Connect to the pluggable database instead with sqlplus sys/ChangeMe_123@localhost:1521/FREEPDB1 as sysdba and run the CREATE USER there. The user is then local to FREEPDB1. Alternatively, from the root you can run ALTER SESSION SET CONTAINER = FREEPDB1; first.
Exercise
A colleague writes the connection string localhost:1521:FREEPDB1 and gets an ORA-12505 error (listener does not currently know of SID). What is wrong, and what is the corrected string?
Show answer
The second colon makes the client treat FREEPDB1 as a SID. A pluggable database has no SID and is reachable only through a service name. Use a slash instead: localhost:1521/FREEPDB1.
Exercise
You are connected as SYS. Which single query tells you both which container and which user your session uses, and which view lists the PDBs and their open mode when you are in the root?
Show answer
Use SELECT sys_context('USERENV', 'CON_NAME') AS container_name, sys_context('USERENV', 'SESSION_USER') AS session_user FROM dual; for the container and user. Use SELECT con_id, name, open_mode FROM v$pdbs; from the root to list PDB$SEED and FREEPDB1 with their open modes.