title: How to switch to the only PDB in Oracle 12+?
body: <p>Using Oracle's <a href="https://github.com/oracle/vagrant-boxes" rel="nofollow noreferrer">vagrant boxes</a>, you can easily add scripts that are run post installation by putting them in the <code>userscripts</code> directory. I want to create my standard users, which is easy (<code>CREATE USER etc...</code>). However, those user needs to be created in the PDB and not in CDB$ROOT.</p>
  
  <p>So, how do I switch from <code>sys / as sysdba</code>, which is connected to CDB$ROOT, to the one and only PDB in the database? The name of the PDB should not be hardcoded, as it is controlled by a parameter in the Vagrantfile. The script should run successfully without intervention. </p>
  
  <p>I got so far, this code is working, but butt-ugly:</p>
  
  <pre><code>COLUMN pdb_name NEW_VALUE mypdb
  SELECT pdb_name
    FROM (
          SELECT pdb_name,
                 RANK() OVER (ORDER BY CREATION_SCN) r
            FROM dba_pdbs p1
           WHERE pdb_name &lt;&gt; 'PDB$SEED'
         )
   WHERE r = 1;
  
  ALTER SESSION SET CONTAINER=&amp;mypdb; 
  </code></pre>
  
  <p>There must be an easier way...</p>