UTL_FILE v16
The UTL_FILE
package reads from and writes to files on the operating system’s file system. A superuser must grant non-superusers EXECUTE
privilege on the UTL_FILE
package before they can use any of the functions or procedures in the package. For example, the following command grants the privilege to user mary
:
Also, the operating system username enterprisedb
must have the appropriate read/write permissions on the directories and files that the UTL_FILE
functions and procedures access. If the required file permissions aren't in place, an exception is thrown in the UTL_FILE
function or procedure.
A handle to the file to write to or read from is used to reference the file. The file handle is defined by a public variable UTL_FILE.FILE_TYPE
in the UTL_FILE
package. A variable of type FILE_TYPE
must be declared to receive the file handle returned by calling the FOPEN
function. The file handle is then used for all subsequent operations on the file.
References to directories on the file system are done using the directory name or alias that's assigned to the directory using the CREATE DIRECTORY
command.
The procedures and functions available in the UTL_FILE
package are listed in the following table.
Function/procedure | Return type | Description |
---|---|---|
FCLOSE(file IN OUT) | n/a | Closes the specified file identified by file . |
FCLOSE_ALL | n/a | Closes all open files. |
FCOPY(location, filename, dest_dir, dest_file [, start_line [, end_line ] ]) | n/a | Copies filename in the directory identified by location to file dest_file in directory dest_dir , from line start_line to line end_line . |
FFLUSH(file) | n/a | Forces data in the buffer to be written to disk in the file identified by file . |
FGETATTR(location, filename, exists, file_length, block_size) | n/a | Retrieves the attributes for a file when its filename and location are given. |
FGETPOS(file) | INTEGER | Returns the relative offset position in the opened file as an integer, in bytes. |
FOPEN(location, filename, open_mode [, max_linesize ]) | FILE_TYPE | Opens file filename in the directory identified by location . |
FOPEN_NCHAR(location, filename, open_mode, max_linesize) | FILE_TYPE | Opens file filename in the directory identified by location to read and/or write Unicode character set data. |
FREMOVE(location, filename) | n/a | Removes the specified file from the file system. |
FRENAME(location, filename, dest_dir, dest_file [, overwrite ]) | n/a | Renames the specified file. |
FSEEK(file, absolute_offset, relative_offset) | n/a | Moves the file pointer within a given file by the number of bytes specified. |
GET_LINE(file, buffer OUT) | n/a | Reads a line of text into the variable buffer from the file identified by file . |
GET_RAW(file, buffer OUT [, len ]) | BYTEA | Reads a RAW string value from a file, keeps those into read buffer, and adjusts the file pointer accordingly by the number of bytes read, ignoring the end-of-file terminator. |
IS_OPEN(file) | BOOLEAN | Determines whether the given file is open. |
NEW_LINE(file [, lines ]) | n/a | Writes an end-of-line character sequence into the file. |
PUT(file, buffer) | n/a | Writes buffer to the given file. PUT doesn't write an end-of-line character sequence. |
PUT_LINE(file, buffer) | n/a | Writes buffer to the given file. An end-of-line character sequence is added by the PUT_LINE procedure. |
PUTF(file, format [, arg1 ] [, ...]) | n/a | Writes a formatted string to the given file. You can specify up to five substitution parameters, arg1,...arg5 , for replacement in format . |
PUT_NCHAR(file, buffer) | n/a | Writes buffer to the given file and converts data to Unicode. PUT doesn't write an end-of-line character sequence. |
PUT_RAW(file, buffer [, autoflush ]) | BOOLEAN | Accepts a RAW data value as input and writes those values to the output buffer. |
EDB Postgres Advanced Server's implementation of UTL_FILE
is a partial implementation when compared to Oracle's version. Only the functions and procedures listed in the table are supported.
UTL_FILE exception codes
If a call to a UTL_FILE
procedure or function raises an exception, you can use the condition name to catch the exception. The UTL_FILE
package reports the following exception codes compatible with Oracle databases.
Exception code | Condition name |
---|---|
-29283 | invalid_operation |
-29285 | write_error |
-29284 | read_error |
-29282 | invalid_filehandle |
-29287 | invalid_maxlinesize |
-29281 | invalid_mode |
-29280 | invalid_path |
Setting file permissions with utl_file.umask
When a UTL_FILE
function or procedure creates a file, the following are the default file permissions:
All permissions are denied on users belonging to the enterprisedb
group as well as all other users. Only the enterprisedb
user has read and write permissions on the created file.
If you want to have a different set of file permissions on files created by the UTL_FILE
functions and procedures, set the utl_file.umask
configuration parameter.
The utl_file.umask
parameter sets the file mode creation mask (or simply the mask) in a manner similar to the Linux umask
command. This parameter is for use only in the EDB Postgres Advanced Server UTL_FILE
package.
Note
The utl_file.umask
parameter isn't supported on Windows systems.
The value specified for utl_file.umask
is a 3- or 4-character octal string that's valid for the Linux umask
command. The setting determines the permissions on files created by the UTL_FILE
functions and procedures. (Refer to any information source regarding Linux or Unix systems for information on file permissions and the use of the umask
command.)
Example
This example sets the file permissions with utl_file.umask
.
First, set up the directory in the file system for the UTL_FILE
package to use. Be sure the applicable operating system account enterprisedb
or postgres
can read and write in the directory.
The CREATE DIRECTORY
command is issued in psql
to create the directory database object using the file system directory you created:
Set the utl_file.umask
configuration parameter. The following setting allows the file owner any permission. Group users and other users are permitted any permission except for the execute permission.
In the same session during which the utl_file.umask
parameter is set to the desired value, run the UTL_FILE
functions and procedures.
The permission settings on the resulting file show that, in addition to the file owner, group users and other users have read and write permissions on the file.
You can also set this parameter on a per-role basis with the ALTER ROLE
command. You can set it for a single database with the ALTER DATABASE
command or for the entire database server instance by setting it in the postgresql.conf
file.
FCLOSE
The FCLOSE
procedure closes an open file.
Parameters
file
Variable of type FILE_TYPE
containing a file handle of the file to close.
FCLOSE_ALL
The FLCLOSE_ALL
procedures closes all open files. The procedure executes successfully even if there are no open files to close.
FCOPY
The FCOPY
procedure copies text from one file to another.
Parameters
location
Directory name of the directory containing the file to copy, as stored in pg_catalog.edb_dir.dirname
.
filename
Name of the source file to copy.
dest_dir
Directory name of the directory to which to copy the file, as stored in pg_catalog.edb_dir.dirname
.
dest_file
Name of the destination file.
start_line
Line number in the source file from which copying begins. The default is 1
.
end_line
Line number of the last line in the source file to copy. If omitted or null, copying goes to the last line of the file.
Examples
This example makes a copy of a file C:\TEMP\EMPDIR\empfile.csv
, which contains a comma-delimited list of employees from the emp
table. The copy, empcopy.csv
, is then listed.
FFLUSH
The FFLUSH
procedure flushes unwritten data from the write buffer to the file.
Parameters
file
Variable of type FILE_TYPE
containing a file handle.
Examples
Each line is flushed after the NEW_LINE
procedure is called.
FGETATTR
The FGETATTR
function retrieves the attributes for a given file. When you enter filename
and location
values, it returns whether the file exists. If the file exists, it also returns file_length
and block_size
values. If the file doesn't exist or an error occurs, the attributes return as NULL
.
Parameters
location
Name of the directory containing the file whose attributes are being retrieved, stored in pg_catalog.edb_dir.dirname
.
filename
Name of the file whose attributes are being retrieved.
exists
Report of whether the file exists.
file_length
Size of the file in bytes.
block_size
System block size of the file in bytes.
Examples
This example retrieves the attributes for the file empfile.csv
:
FGETPOS
The FGETPOS
function returns the relative offset position in the opened file as an integer, in bytes.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the opened file.
Examples
Thus example finds the position of the file empfile.csv
:
FOPEN
The FOPEN
function opens a file for I/O.
Parameters
location
Directory name of the directory containing the file to open, as stored in pg_catalog.edb_dir.dirname
.
filename
Name of the file to open.
open_mode
Mode in which to open the file. Modes are:
max_linesize
Maximum size of a line in characters. In read mode, an exception is thrown if you try to read a line exceeding max_linesize
. In write and append modes, an exception is thrown if you try to write a line exceeding max_linesize
. The end-of-line characters aren't included in determining if the maximum line size is exceeded. This behavior isn't compatible with Oracle databases. Oracle counts the end-of-line characters.
filetype
Variable of type FILE_TYPE
containing the file handle of the opened file.
FOPEN_NCHAR
The FOPEN_NCHAR
procedure opens the file filename
in the directory identified by location
to read and/or write Unicode character set data.
Parameters
location
Name of the directory containing the file to open, stored in pg_catalog.edb_dir.dirname
.
filename
Name of the file to open.
open_mode
Mode in which to open the file. Modes are:
max_linesize
Maximum size of a line in characters. In read mode, an exception is thrown if you try to read a line exceeding max_linesize
. In write and append modes, an exception is thrown if you try to write a line exceeding max_linesize
. The end-of-line characters aren't included in determining if the maximum line size is exceeded. This behavior isn't compatible with Oracle databases. Oracle counts the end-of-line characters.
Examples
Thus example opens the file empfile1.csv
:
FREMOVE
The FREMOVE
procedure removes a file from the system.
An exception is thrown if the file doesn't exist.
Parameters
location
Directory name of the directory containing the file to remove, as stored in pg_catalog.edb_dir.dirname
.
filename
Name of the file to remove.
Examples
This example removes the file empfile.csv
:
FRENAME
The FRENAME
procedure renames a file, effectively moving a file from one location to another.
Parameters
location
Directory name of the directory containing the file to rename, as stored in pg_catalog.edb_dir.dirname
.
filename
Name of the source file to rename.
dest_dir
Directory name of the directory to which to locate the renamed file, as stored in pg_catalog.edb_dir.dirname
.
dest_file
New name of the file.
overwrite
Replaces any existing file named dest_file
in dest_dir
if set to TRUE
. An exception is thrown if set to FALSE
(the default).
Examples
This example renames a file, C:\TEMP\EMPDIR\empfile.csv
, containing a comma-delimited list of employees from the emp
table. The renamed file, C:\TEMP\NEWDIR\newemp.csv
, is then listed.
FSEEK
The FSEEK
procedure moves the file pointer within a given file by the number of bytes specified.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file where the file pointer is being moved.
absolute_offset
The absolute number of bytes to move the file pointer.
relative_offset
The relative number of bytes to move the file pointer.
Examples
This examples moves the file pointer in the file empfile.csv
:
GET_LINE
The GET_LINE
procedure reads a line of text from a given file up to but not including the end-of-line terminator. A NO_DATA_FOUND
exception is thrown when there are no more lines to read.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the opened file.
buffer
Variable to receive a line from the file.
Examples
The following anonymous block reads through and displays the records in file empfile.csv
.
GET_RAW
The GET_RAW
procedure reads a RAW
string value from a file, keeps those into read buffer, and adjusts the file pointer accordingly by the number of bytes read. GET_RAW
ignores the end-of-file terminator. INVALID_FILEHANDLE
, INVALID_OPERATION
, and READ_ERROR
exceptions are thrown when there are no more lines to read.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the opened file.
buffer
Assign RAW
data from the file to the read buffer.
len
The number of bytes read from a file. Default is NULL
. If NULL
, len
tries to read a maximum of 32767 RAW
bytes.
Examples
This example attempts to read a RAW
string value from the file.
IS_OPEN
The IS_OPEN
function determines whether a file is open.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to test.
status
TRUE
if the file is open, FALSE
otherwise.
NEW_LINE
The NEW_LINE
procedure writes an end-of-line character sequence in the file.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write end-of-line character sequences.
lines
Number of end-of-line character sequences to write. The default is 1
.
Examples
This example writes a file containing a double-spaced list of employee records.
This file is then displayed:
PUT
The PUT
procedure writes a string to the given file. No end-of-line character sequence is written at the end of the string. Use the NEW_LINE
procedure to add an end-of-line character sequence.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write the given string.
buffer
Text to write to the specified file.
Examples
This example uses the PUT
procedure to create a comma-delimited file of employees from the emp
table.
The following are the contents of empfile.csv
:
PUT_LINE
The PUT_LINE
procedure writes a single line to a file, including an end-of-line character sequence.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write the line.
buffer
Text to write to the file.
Examples
This example uses the PUT_LINE
procedure to create a comma-delimited file of employees from the emp
table.
The following are the contents of empfile.csv
:
PUTF
The PUTF
procedure writes a formatted string to a file.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write the formatted line.
format
String to format the text written to the file. The special character sequence %s
is substituted by the value of arg
. The special character sequence \n
indicates a new line. In EDB Postgres Advanced Server, specify a new line character with two consecutive backslashes instead of one: \\n
. This characteristic isn't compatible with Oracle databases.
arg1
Up to five arguments, arg1
,...arg5
, to substitute in the format string for each occurrence of %s
. The first arg is substituted for the first occurrence of %s
, the second arg is substituted for the second occurrence of %s
, and so on.
Examples
The following anonymous block produces formatted output containing data from the emp
table.
Note
The E literal syntax and double backslashes for the new-line character sequence in the format string aren't compatible with Oracle databases.
The following are the contents of empfile.csv
:
PUT_NCHAR
The PUT_NCHAR
procedure writes buffer
to the given file and converts data to Unicode. PUT
doesn't write an end-of-line character sequence.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write the line.
buffer
Variable to write RAW
data to the output buffer.
Examples
This example writes buffer
to the file empfile.csv
and converts data to Unicode:
PUT_RAW
The PUT_RAW
procedure accepts a RAW
data value and writes those values to the output buffer. INVALID_FILEHANDLE
, INVALID_OPERATION
, WRITE_ERROR
, and VALUE_ERROR
exceptions are thrown when there are no more lines to read.
Parameters
file
Variable of type FILE_TYPE
containing the file handle of the file to which to write the line.
buffer
Variable to write RAW
data to the output buffer.
autoflush
If TRUE
, performs a flush after writing the value to the output buffer. By default, autoflush
is FALSE
.
Examples
This example writes the RAW
data value from the emp
table.