Oracle CREATE DIRECTORY Command Explained

Oracle CREATE DIRECTORY Command Explained

The Oracle CREATE DIRECTORY command creates a directory object β€” a logical alias inside the Oracle database that points to a real folder on the server's file system. This is one of the most used setup steps for Oracle Data Pump exports and imports, UTL_FILE file operations, BFILEs, and external tables.

What Is an Oracle Directory Object?

A directory object does not create a real folder on the operating system. It only creates a pointer β€” or alias β€” inside Oracle that maps a name you choose to a physical path on the database server. This keeps your PL/SQL code and Data Pump jobs clean because you use the Oracle directory name instead of hard-coding full OS paths in your scripts.

Oracle introduced the concept of directory objects in Oracle 8.0. Since then, the feature has grown to support Data Pump (expdp/impdp), UTL_FILE, DBMS_LOB, OCI file functions, and external table data locations.


The Code

1CREATE DIRECTORY my_dir AS '/home/oracle/larrye';

This single line is all you need to register the directory with Oracle. Once created, it is stored as a schema-independent object owned by SYS.


Breakdown of the Code

PartMeaning
CREATE DIRECTORYThe SQL DDL command to create a new directory object in Oracle
my_dirThe Oracle directory object name (alias) β€” max 30 bytes, no schema prefix allowed
ASKeyword linking the alias to the OS path
'/home/oracle/larrye'The full, case-sensitive path on the database server file system β€” single quotes required

Full Syntax Reference

The complete syntax supported by Oracle is:

1CREATE [OR REPLACE] DIRECTORY directory_name AS 'path_name';
  • OR REPLACE β€” Re-creates the directory object if it already exists, without needing to drop and re-grant privileges first
  • directory_name β€” The Oracle alias name for the directory
  • 'path_name' β€” Full OS path on the server; the path is case-sensitive due to the single quotes

Required Privileges

You must have the CREATE ANY DIRECTORY system privilege to run this command. This is a powerful DBA-level privilege and should be granted carefully.

1-- Grant the privilege to a user
2GRANT CREATE ANY DIRECTORY TO your_username;

Grant Read and Write on the Directory

Creating the directory object is only the first step. You must also grant READ and/or WRITE permissions on the directory object to the users or roles that need to use it.

1-- Grant read permission
2GRANT READ ON DIRECTORY my_dir TO your_username;
3
4-- Grant write permission
5GRANT WRITE ON DIRECTORY my_dir TO your_username;
6
7-- Grant both read and write
8GRANT READ, WRITE ON DIRECTORY my_dir TO your_username;

Important: Oracle directory privileges and OS-level file system permissions are independent. Both must be configured correctly. For example, if Oracle grants READ on the directory object but the OS folder does not have read permission for the Oracle process, the operation will still fail with an error.


Use with Oracle Data Pump (expdp / impdp)

The most common real-world use of CREATE DIRECTORY is setting up a location for Data Pump exports and imports. Here is a typical Data Pump directory setup:

1-- Create the Data Pump directory
2CREATE OR REPLACE DIRECTORY expdp_dir AS '/home/oracle/larrye';
3
4-- Grant access to Data Pump roles
5GRANT READ, WRITE ON DIRECTORY expdp_dir TO exp_full_database;
6GRANT READ, WRITE ON DIRECTORY expdp_dir TO imp_full_database;
7GRANT READ, WRITE ON DIRECTORY expdp_dir TO system;

Then use the directory in your expdp command:

1expdp system/password DIRECTORY=expdp_dir DUMPFILE=full_export.dmp LOGFILE=export.log FULL=Y

Use with UTL_FILE

Directory objects are also required when using UTL_FILE to read or write flat files from PL/SQL.

 1CREATE DIRECTORY my_dir AS '/home/oracle/larrye';
 2
 3DECLARE
 4  file_handle UTL_FILE.FILE_TYPE;
 5BEGIN
 6  file_handle := UTL_FILE.FOPEN('MY_DIR', 'output.txt', 'W');
 7  UTL_FILE.PUT_LINE(file_handle, 'Hello from Oracle PL/SQL');
 8  UTL_FILE.FCLOSE(file_handle);
 9END;
10/

Note that the directory name passed to UTL_FILE.FOPEN is always uppercase inside Oracle, regardless of how you typed it when you created it.


View Existing Directory Objects

To list all directory objects already in the database, query the data dictionary:

1-- View all directories (DBA view)
2SELECT directory_name, directory_path
3FROM dba_directories
4ORDER BY directory_name;
5
6-- View directories you own or have access to
7SELECT directory_name, directory_path
8FROM all_directories
9ORDER BY directory_name;

Replace or Modify a Directory

To change the path of an existing directory without dropping it and re-granting all privileges, use OR REPLACE:

1CREATE OR REPLACE DIRECTORY my_dir AS '/home/oracle/new_path';

Users who already have privileges on the old directory keep those privileges after the replace.


Drop a Directory

To remove a directory object from Oracle:

1DROP DIRECTORY my_dir;

This does not delete the actual OS folder. It only removes the Oracle alias.


Key Points and Insights

  • The CREATE DIRECTORY command creates an Oracle alias only β€” it does not create the physical folder on the OS. You must create the actual OS folder separately (e.g., mkdir /home/oracle/larrye on Linux).
  • The OS path is case-sensitive on Linux/Unix systems because it is stored in single quotes.
  • Directory objects are owned by SYS and are not schema-specific β€” they cannot be prefixed with a schema name.
  • The CREATE ANY DIRECTORY privilege is a strong system privilege β€” only DBAs should hold it.
  • Both Oracle-level and OS-level permissions must allow access; Oracle does not bypass OS security.
  • OR REPLACE is the safest way to update a directory path because it preserves existing grants.
  • For Data Pump jobs, the directory must have both READ and WRITE grants on the relevant roles or users.
  • Directory object names have a maximum length of 30 bytes.

References

Posts in this series