Discussion:
Escape char in pg_ls_dir
lgcaracol
2017-12-29 10:00:56 UTC
Permalink
Hi,

I'm new to postgresql. I have a database running on a windows machine and I
need to use pg_ls_dir to list the contents of a folder, but I can't because
of the backslash (\), as it's not allowed.
If I run select pg_ls_dir('c:') I can see the listing of C:\, but I need to
check another folder.
If I run select pg_ls_dir(E'c:\inetpub'), I got an error "could not open
directory "c:inetpub": No such file or directory. So I now the \ is not
being used.
If I run select pg_ls_dir('c:\\inetpub'), it says "absolute path not
allowed".

Anyone can give me a hint of how can I do this?

Thanks



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-novice-f2132464.html
Tom Lane
2017-12-29 15:45:17 UTC
Permalink
Post by lgcaracol
I'm new to postgresql. I have a database running on a windows machine and I
need to use pg_ls_dir to list the contents of a folder, but I can't because
of the backslash (\), as it's not allowed.
If I run select pg_ls_dir('c:') I can see the listing of C:\, but I need to
check another folder.
If I run select pg_ls_dir(E'c:\inetpub'), I got an error "could not open
directory "c:inetpub": No such file or directory. So I now the \ is not
being used.
If I run select pg_ls_dir('c:\\inetpub'), it says "absolute path not
allowed".
That's intentional: the documentation for pg_ls_dir and friends says in
so many words "Only files within the database cluster directory and the
log_directory can be accessed". (It seems like a Windows-specific bug
to me that we allow a drive prefix.)

If you want access to random bits of the server's filesystem, I'd suggest
installing plperlu or plpythonu and writing up your own access functions.
"List this directory" should be just a couple of lines in either language.

BTW, your confusion about how to write a backslash in a string literal
would perhaps be dispelled here:

https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS

Neither of the ways you used above is quite right; you should either
write E'c:\\inetpub' or just 'c:\inetpub'.

regards, tom lane

Loading...