Sub topic 10
Saving and reusing connections in SQLcl
Typing a full Easy Connect string every time you start SQLcl is slow and invites typos. SQLcl can store connections under a name and reconnect with one short command. This chapter covers how saving works, what gets stored, how to manage saved entries, and the security trade-offs of storing passwords.
Prerequisites
This chapter assumes you can already start SQLcl and connect with Easy Connect syntax (user@//host:port/service). It also assumes you know the difference between a service name and a SID, and you have an account on a database you can reach. If any of that is unfamiliar, review the earlier chapters on the connection model and the CONNECT command first.
What a saved connection is
A saved connection is a named entry in SQLcl's connection store. The entry records the pieces needed to open a session:
- The connect identifier: host, port, and service name, or a TNS alias.
- The user name.
- Optionally, the password, stored in encrypted form.
- Optionally, other settings such as a role or a wallet location.
The store lives in a per-user directory on disk, under .dbtools in your home directory. SQLcl manages the files, so you normally use the connect and connmgr commands and do not edit them by hand.
A saved name is only a shortcut. When you connect by name, SQLcl reads the entry and performs the same connection it would perform if you typed out the full string. The listener, service, and credentials are checked exactly as before.
Saving a connection
Add -save followed by a name to a normal CONNECT command:
SQL> connect -save orclpdb_hr -savepwd hr@//dbhost.example.com:1521/orclpdb1.example.comTwo options do different jobs:
| Option | Effect |
|---|---|
| `-save name` | Connects, and if the connection succeeds, stores the identifier and user name under `name`. |
| `-savepwd` | Also stores the password, encrypted. |
Without -savepwd, SQLcl stores no password. When you reconnect by name, it prompts for the password. This is the safer default, and it still saves you from typing the long identifier.
The save happens only after a successful connect. A wrong password or an unreachable listener means nothing is stored, so a saved name always refers to a connection that worked at least once.
If the name already exists, SQLcl refuses to overwrite it unless you add -replace:
SQL> connect -save orclpdb_hr -replace -savepwd hr@//dbhost.example.com:1521/orclpdb1.example.comNaming conventions
Names are free text, but a consistent pattern pays off once you have a dozen entries. A pattern such as database_user (orclpdb_hr, orclpdb_scott, prod_reporting) tells you both where the session goes and who you are. Avoid spaces. If you use a name containing special characters, you will need to quote it.
Reconnecting by name
Use -name to connect from the store:
SQL> connect -name orclpdb_hr
Connected.If you saved the entry with -savepwd, SQLcl connects immediately. If not, it asks for the password:
SQL> connect -name orclpdb_hr
Password? (**********?) ********
Connected.You can also use the name when starting SQLcl, so you never see the identifier at all:
sql -name orclpdb_hrWhichever way you connect, run a verification query afterwards. A saved name hides the details, so it is easy to forget which database it points to.
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;Listing and managing saved connections
The connmgr command manages the store. List the saved names first:
SQL> connmgr listThe other subcommands you will use most often are:
| Command | Purpose |
|---|---|
| `connmgr list` | Show the names of all saved connections. |
| `connmgr show name` | Show the details of one saved connection. |
| `connmgr delete name` | Remove one saved connection. |
| `connmgr export file` | Write saved connections to a file. |
| `connmgr import file` | Load connections from a file. |
Subcommands vary somewhat between SQLcl releases. Type connmgr with no arguments, or help connmgr, to see what your installed version supports.
Export and import are useful for moving your setup to a new machine or sharing connection definitions with a team. Check what the exported file contains before you share it, because the file may include encrypted passwords for entries saved with -savepwd. Remove those entries first, or save them again without a password.
What the store does not fix
Saving a connection does not change how the connection behaves. Three consequences follow.
- A stale entry fails like any other connection. If the database moves to a new host or the service is renamed, the saved identifier still points at the old location. You get the same errors as with a typed string, such as ORA-12541 or ORA-12514. Fix it by saving again with
-replace. - A changed password breaks stored credentials. If the account password changes, an entry saved with
-savepwdfails with ORA-01017. Save the entry again with the new password. - A TNS alias is resolved at connect time. If you save a connection that uses an alias such as
hr@ORCLPDB, the alias is looked up intnsnames.oraeach time. Changing that file changes where the saved connection goes. Saving the full Easy Connect string avoids that dependency, at the cost of repeating host details in the store.
Credential security
A saved password is encrypted, but encrypted is not the same as secret from anyone who can reach your account. The encryption is intended to keep the password from being read casually in a file. Anyone who can run SQLcl as you, or copy your .dbtools directory, can probably use the saved connection. Treat the store as sensitive.
Guidelines that follow from this:
- Use
-savepwdonly for low-risk databases, such as a local development container or a sandbox. - For production or shared accounts, save the connection without a password and type it at the prompt.
- Do not put a password in the command line, as in
connect hr/secret@.... It can appear in shell history and in process listings. - Restrict file permissions on your home directory and the
.dbtoolsdirectory. On Linux and macOS,chmod -R go-rwx ~/.dbtoolsremoves access for other users. - Do not commit exported connection files to source control.
A practical workflow
A typical set-up for someone working with a development database and a production database looks like this:
SQL> connect -save dev_hr -savepwd hr@//devhost.example.com:1521/devpdb.example.com
SQL> connect -save prod_hr hr@//prodhost.example.com:1521/prodpdb.example.com
SQL> connmgr listThe development entry connects instantly. The production entry asks for a password each time, which also serves as a small pause before working on a live system. Run the verification query after each reconnect, and confirm server_host and service_name match what you expect.
Common problems
- "Connection name already exists." Add
-replace, or pick another name. - The name is not found. Run
connmgr list. Names are case sensitive in some versions, so check the exact spelling. - The prompt appears even though you used
-savepwd. The entry was probably saved without it, or was saved again later without the option. Save it again with-replace -savepwd. - ORA-12154 on a saved TNS alias. The alias no longer resolves. Check that
TNS_ADMINpoints to the directory holding the righttnsnames.ora. - ORA-01017 after a password change. Replace the saved entry.
Exercise
You saved a connection with connect -save prod_hr hr@//prodhost.example.com:1521/prodpdb.example.com (no -savepwd). Later, the DBA moves the database to newhost.example.com and you also change the HR password. What will happen when you run connect -name prod_hr, and what single command fixes the entry?
Show answer
SQLcl prompts for a password, then fails because the stored identifier still points to prodhost (typically ORA-12541 if nothing listens there, or ORA-12514 if a listener there does not know the service). The password change does not matter until the host is right. Fix it with connect -save prod_hr -replace hr@//newhost.example.com:1521/prodpdb.example.com, entering the new password when asked.
Exercise
Why is connect -save dev_hr -savepwd hr@... acceptable for a local container but a poor choice for a production account?
Show answer
-savepwd stores the password, encrypted, in the connection store in your home directory. Anyone who can run SQLcl as you or copy that directory can probably reuse the connection without knowing the password. For a throwaway local database that risk is small. For production it exposes a real account, so save the connection without -savepwd and enter the password at the prompt.