Help Center > > Developer Guide> SQL Reference> Functions and Operators> Universal File Access Functions

Universal File Access Functions

Updated at: Jul 15, 2020 GMT+08:00

Universal file access functions provide local access interfaces for files on a database server. Only files in the database cluster directory and the log_directory directory can be accessed. Use a relative path for files in the cluster directory, and a path matching the log_directory configuration setting for log files. Only database system administrators can use these functions.

  • pg_ls_dir(dirname text)

    Description: Lists files in a directory.

    Return type: setof text

    Note: pg_ls_dir returns all the names in the specified directory, except the special entries "." and "..".

    For example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    SELECT pg_ls_dir('./');
          pg_ls_dir       
    ----------------------
     .postgresql.conf.swp
     postgresql.conf
     pg_tblspc
     PG_VERSION
     pg_ident.conf
     core
     server.crt
     pg_serial
     pg_twophase
     postgresql.conf.lock
     pg_stat_tmp
     pg_notify
     pg_subtrans
     pg_ctl.lock
     pg_xlog
     pg_clog
     base
     pg_snapshots
     postmaster.opts
     postmaster.pid
     server.key.rand
     server.key.cipher
     pg_multixact
     pg_errorinfo
     server.key
     pg_hba.conf
     pg_replslot
     .pg_hba.conf.swp
     cacert.pem
     pg_hba.conf.lock
     global
     gaussdb.state
    (32 rows)
    
  • pg_read_binary_file(filename text [, offset bigint, length bigint,missing_ok boolean])

    Description: Returns the content of a binary file.

    Return type: bytea

    Note: pg_read_binary_file is similar to pg_read_file, except that the result is a bytea value; accordingly, no encoding checks are performed. In combination with the convert_from function, this function can be used to read a file in a specified encoding:

    1
    SELECT convert_from(pg_read_binary_file('filename'), 'UTF8');
    
  • pg_stat_file(filename text)

    Description: Returns status information about a file.

    Return type: record

    Note: pg_stat_file returns a record containing the file size, last access timestamp, last modification timestamp, last file status change timestamp, and a Boolean value indicating if it is a directory. Typical use cases are as follows:

    1
    SELECT * FROM pg_stat_file('filename');
    
    1
    SELECT (pg_stat_file('filename')).modification;
    

    For example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    SELECT * FROM pg_stat_file('postmaster.pid');
     
     size |         access         |      modification      |         change         
    | creation | isdir 
    ------+------------------------+------------------------+------------------------
    +----------+-------
      117 | 2017-06-05 11:06:34+08 | 2017-06-01 17:18:08+08 | 2017-06-01 17:18:08+08 
    |          | f
    (1 row)
    
    1
    2
    3
    4
    5
    SELECT (pg_stat_file('postmaster.pid')).modification;
          modification      
    ------------------------
     2017-06-01 17:18:08+08
    (1 row)
    

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel