How can I write SQL to read an XML file into a PostgreSQL XML
value?
PostgreSQL has a native XML data type with the XMLPARSE
function to parse a text string to that type. It also has ways to read data from the filesystem; the COPY
statement, among others.
But I don't see a way to write native PostgreSQL SQL statements to read the content from a filesystem entry and use that to populate an XML
value. How can I do this?
Best Answer
Similar to this answer to a previous question, and if you don't want the restrictions of
pg_read_file()
(in short:pg_read_file
can't read files outside the database directory, and reads text in the current session's character encoding).This function works for any path, but needs to be created as superuser:
lo_get
was introduced in 9.4 so for older versions you would need:then: