后端技巧

查看数据目录中的文件

场景:您需要检查服务器数据目录中的文件,但没有服务器登录权限,或者您的服务器帐户没有必要的权限。

解决方案:使用 PostgreSQL 的通用文件访问函数,这些函数允许您列出和读取数据目录内的(但不能超出)文件。

test=# SELECT pg_ls_dir('.');
    pg_ls_dir    
-----------------
 base
 global
 pg_clog
 pg_hba.conf
 pg_ident.conf
(...)
 pg_twophase
 PG_VERSION
 pg_wal
 postgresql.conf
 postmaster.opts
 postmaster.pid
(18 rows)

 

test=# SELECT pg_read_file('postgresql.conf');
                                  pg_read_file                                                   
-------------------------------------------------------------------------------
 # -----------------------------                                              +
 # PostgreSQL configuration file                                              +
 # -----------------------------                                              +
 #                                                                            +
(...)
 #-----------------------------------------------------------                 +
 # CONNECTIONS AND AUTHENTICATION                                             +
 #-----------------------------------------------------------                 +
                                                                              +
 # - Connection Settings -                                                    +
                                                                              +
 #listen_addresses = 'localhost'         # what IP address(es) to listen on   +
                                         # (change requires restart)          +
 #port = 5432                            # (change requires restart)          +
 max_connections = 20                    # (change requires restart)          +

如果您需要访问数据目录之外的服务器文件,可以使用 file_fdw 扩展。