Day 5: Cloning, Unplugging and Plugging Oracle Pluggable Databases
1. Objective
A hands-on Oracle 19c lab for DBAs: clone a pluggable database, save its open state, unplug it to an XML manifest and plug it back in on Oracle Linux. On ora19-prod01 we clone SALESPDB to a new PDB called SALESTEST, make it reopen automatically after restarts, unplug it, check it is compatible, plug it back in and then drop it, leaving SALESCDB exactly as we found it.
2. The concept in two minutes
SALESCDB is a container database (CDB): one instance, one set of control files and one redo stream. SALESPDB is a pluggable database (PDB) inside it, with its own datafiles, users and service. Because a PDB is a self-contained set of files, we can copy, move or detach it without touching other PDBs.
- Clone —
CREATE PLUGGABLE DATABASE … FROMcopies a PDB. A hot clone (source open read write) needs the CDB in ARCHIVELOG mode and local undo. If the CDB is in NOARCHIVELOG mode, the source must be open read only during the copy. - Unplug — closes the PDB and writes an XML manifest that describes its files and options.
- Plug in —
CREATE PLUGGABLE DATABASE … USING 'file.xml'attaches the files again (NOCOPY uses them in place). - SAVE STATE — records the open mode so the PDB reopens automatically when the CDB restarts.
Licensing note: in Oracle Database 19c, a CDB may hold up to three user-created PDBs without the Multitenant option. Today we never have more than two (SALESPDB and SALESTEST).
▶ Watch on YouTube: https://youtu.be/HEil_t6Aj0Y
3. Architecture
4. Prerequisites
| Item | Lab value |
|---|---|
| Host | ora19-prod01.dba365.lab (192.168.56.11), Oracle Linux, 2 vCPU, 8 GB RAM, 20 GB free in /u02 |
| Software | Oracle Database 19c (19.19 or later RU on Oracle Linux 9), ORACLE_HOME /u01/app/oracle/product/19.0.0/dbhome_1 |
| Database | CDB SALESCDB with PDB SALESPDB open read write (local undo, the DBCA default) |
| OS users | root to log in, then oracle (group dba) for every database command |
| Builds on | Day 2: Installing Oracle Database and creating the SALES container database |
Directory /u02/oradata must exist and be owned by oracle:oinstall (created on Day 1).
5. Implementation
Step 1 — Switch to oracle and connect to CDB$ROOT (ora19-prod01, root → oracle)
[root@ora19-prod01 ~]# su - oracle
[oracle@ora19-prod01 ~]$ export ORACLE_SID=SALESCDB
[oracle@ora19-prod01 ~]$ export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
[oracle@ora19-prod01 ~]$ export PATH=$ORACLE_HOME/bin:$PATH
[oracle@ora19-prod01 ~]$ sqlplus / as sysdba
SQL> SHOW CON_NAME
CON_NAME
------------------------------
CDB$ROOT
What this does: switches from root to the oracle software owner and connects to the root container, where all PDB create, unplug and drop statements must run.
Step 2 — Check log mode and local undo (oracle, SQL*Plus)
SQL> SELECT log_mode FROM v$database;
LOG_MODE
------------
NOARCHIVELOG
SQL> COLUMN property_value FORMAT a14
SQL> SELECT property_value FROM database_properties
2 WHERE property_name = 'LOCAL_UNDO_ENABLED';
PROPERTY_VALUE
--------------
TRUE
What this does: tells us which clone method is possible. With NOARCHIVELOG (our lab until Day 19), the source PDB must be open read only during the clone. If your CDB shows ARCHIVELOG and TRUE, you may skip the read-only reopen in Step 3 (hot clone).
Step 3 — Clone SALESPDB to SALESTEST (oracle, SQL*Plus)
SQL> ALTER PLUGGABLE DATABASE salespdb CLOSE IMMEDIATE;
SQL> ALTER PLUGGABLE DATABASE salespdb OPEN READ ONLY;
SQL> ALTER SESSION SET db_create_file_dest = '/u02/oradata';
SQL> CREATE PLUGGABLE DATABASE salestest FROM salespdb;
Pluggable database created.
SQL> ALTER PLUGGABLE DATABASE salespdb CLOSE IMMEDIATE;
SQL> ALTER PLUGGABLE DATABASE salespdb OPEN;
What this does: copies every SALESPDB datafile into new Oracle-managed files under /u02/oradata/SALESCDB/<GUID>/datafile and gives SALESTEST its own GUID and default service, then returns SALESPDB to read write.
Lab-only note: closing SALESPDB means a short outage for its users. In production, put the CDB in ARCHIVELOG mode (it should be anyway, for RMAN) and hot-clone with the source open. Setting db_create_file_dest at session level affects only this session; the production alternative is to set it for the CDB (or use FILE_NAME_CONVERT) under your file-placement standard.
Step 4 — Open SALESTEST and save state (oracle, SQL*Plus)
SQL> ALTER PLUGGABLE DATABASE salestest OPEN;
SQL> ALTER PLUGGABLE DATABASE salespdb SAVE STATE;
SQL> ALTER PLUGGABLE DATABASE salestest SAVE STATE;
SQL> COLUMN con_name FORMAT a15
SQL> SELECT con_name, state FROM dba_pdb_saved_states;
CON_NAME STATE
--------------- --------------
SALESPDB OPEN
SALESTEST OPEN
What this does: records the current open mode, so after a restart of SALESCDB both PDBs open read write without manual action.
Step 5 — Unplug SALESTEST (oracle, SQL*Plus)
SQL> ALTER PLUGGABLE DATABASE salestest DISCARD STATE;
SQL> ALTER PLUGGABLE DATABASE salestest CLOSE IMMEDIATE;
SQL> ALTER PLUGGABLE DATABASE salestest
2 UNPLUG INTO '/u02/oradata/salestest.xml';
Pluggable database altered.
SQL> DROP PLUGGABLE DATABASE salestest KEEP DATAFILES;
Pluggable database dropped.
What this does: removes the saved state, closes the PDB and writes the XML manifest (file list, version, options). KEEP DATAFILES (the default) removes SALESTEST from the CDB but leaves its files on disk for the plug-in.
Step 6 — Check plug compatibility (oracle, SQL*Plus)
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
2 IF DBMS_PDB.CHECK_PLUG_COMPATIBILITY(
3 '/u02/oradata/salestest.xml', 'SALESTEST')
4 THEN DBMS_OUTPUT.PUT_LINE('COMPATIBLE: YES');
5 ELSE DBMS_OUTPUT.PUT_LINE('COMPATIBLE: NO');
6 END IF;
7 END;
8 /
COMPATIBLE: YES
PL/SQL procedure successfully completed.
What this does: compares the manifest with this CDB (version, patches, options, character set). If the result is NO, query PDB_PLUG_IN_VIOLATIONS for the reason and the suggested action.
Step 7 — Plug SALESTEST back in (oracle, SQL*Plus)
SQL> CREATE PLUGGABLE DATABASE salestest
2 USING '/u02/oradata/salestest.xml'
3 NOCOPY TEMPFILE REUSE;
Pluggable database created.
SQL> ALTER PLUGGABLE DATABASE salestest OPEN;
What this does: registers the PDB again from its manifest. NOCOPY uses the datafiles where they are; TEMPFILE REUSE reuses a temp file of the same name if one exists, or creates it (DROP … KEEP DATAFILES removes the old temp file). Because the files are used in place, keep a backup before plugging into a different CDB in production.
Step 8 — Clean up (oracle, then back to root)
SQL> ALTER PLUGGABLE DATABASE salestest CLOSE IMMEDIATE;
SQL> DROP PLUGGABLE DATABASE salestest INCLUDING DATAFILES;
Pluggable database dropped.
SQL> EXIT
[oracle@ora19-prod01 ~]$ rm /u02/oradata/salestest.xml
[oracle@ora19-prod01 ~]$ exit
[root@ora19-prod01 ~]#
What this does: deletes the test PDB and its files, so the lab ends with SALESCDB and SALESPDB only.
6. What each step does (summary)
- Step 1–2: connect as oracle to CDB$ROOT and decide hot vs read-only clone.
- Step 3: clone. Step 4: auto-open after restart. Step 5: unplug to XML.
- Step 6: compatibility check. Step 7: plug in. Step 8: clean up.
7. Validation
Run after Step 7 (oracle, SQL*Plus, CDB$ROOT):
SQL> SHOW PDBS
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 SALESPDB READ WRITE NO
4 SALESTEST READ WRITE NO
SQL> SELECT pdb_name, status FROM dba_pdbs ORDER BY pdb_id;
-- expected: PDB$SEED, SALESPDB and SALESTEST all NORMAL
SQL> SELECT name, cause, message FROM pdb_plug_in_violations
2 WHERE status <> 'RESOLVED';
-- expected: no rows selected
RESTRICTED must be NO. The CON_ID of SALESTEST may differ on your system. After Step 8, SHOW PDBS lists only PDB$SEED and SALESPDB.
8. Troubleshooting
| Error / symptom | Cause | Fix |
|---|---|---|
| ORA-65040: operation not allowed from within a pluggable database | Session is connected to a PDB | ALTER SESSION SET CONTAINER = CDB$ROOT; or reconnect with sqlplus / as sysdba |
| ORA-65016: FILE_NAME_CONVERT must be specified | No OMF destination and no PDB_FILE_NAME_CONVERT | Set db_create_file_dest (Step 3) or add a FILE_NAME_CONVERT clause |
| ORA-65025: Pluggable database SALESTEST is not closed on all instances | Unplug or drop attempted while open | ALTER PLUGGABLE DATABASE salestest CLOSE IMMEDIATE; then retry |
| ORA-65012: Pluggable database SALESTEST already exists | A previous run left SALESTEST behind | Run Step 8, or use another PDB name |
| ORA-65122: Pluggable database GUID conflicts with the GUID of an existing container | Plugging in while the original PDB (same GUID) is still in the CDB | Drop the original first (Step 5), or plug in with AS CLONE to get a new GUID |
9. Production considerations
Lab vs production
- Clone method: lab uses a read-only source because SALESCDB is NOARCHIVELOG. Production CDBs should run in ARCHIVELOG mode, which allows a hot clone with no outage for SALESPDB users.
- Security: a clone copies all users, passwords and data. Mask or remove sensitive data before giving a test PDB to developers, lock unused accounts, and grant access to the new service only to the hosts that need it.
- Least privilege: use a named DBA account with SYSDBA only where needed; do not share the oracle OS password.
- Licensing: 19c allows up to three user PDBs per CDB without the Multitenant option; count PDBs before cloning.
- Sizing: a full clone needs the same space as the source; check free space in the target file system first.
- HA/backup: take an RMAN backup of the source before unplugging, and back up a newly plugged-in PDB straight away (Day 23). With Data Guard, plan standby file handling before creating PDBs.
- Rollback: keep the XML manifest and datafiles until the plug-in is validated; if it fails, drop the new PDB with KEEP DATAFILES and plug in again from the same manifest.
10. Practice exercise
- Clone SALESPDB to SALESDEV using
FILE_NAME_CONVERTinstead ofdb_create_file_dest. Hint: runSELECT name FROM v$datafile WHERE con_id = 3;to see the source path, then convert that directory to a new one. - Restart SALESCDB (
SHUTDOWN IMMEDIATE,STARTUP) and prove SAVE STATE works. Hint: compareSHOW PDBSwith and without a row indba_pdb_saved_states.
Next lab: Day 6 — Databases, roles, schemas and privileges in PostgreSQL (hrdb on pg17-prod01).
Builds on: Day 2 — Installing Oracle Database and creating the SALES container database.
Need help managing PDBs or a multitenant migration? See our remote Oracle DBA services.
Comments
Post a Comment