How to export full-text files with SQL?

后端 未结 2 1123
悲哀的现实
悲哀的现实 2021-01-13 18:31

There are an easy way to import/export full-text fields as files?

  • that solve the problem of \"load as multiple lines\".
    Trying with SQ

相关标签:
2条回答
  • 2021-01-13 18:53

    You can use plpythonu f.open(), f.write(), f.close() within a postgres function to write to a file.

    Language extension would need to be installed.,

    https://www.postgresql.org/docs/8.3/static/plpython.html

    Working example from the mailing list. https://www.postgresql.org/message-id/flat/20041106125209.55697.qmail%40web51806.mail.yahoo.com#20041106125209.55697.qmail@web51806.mail.yahoo.com

    for example plpythonu

    CREATE FUNCTION makefile(p_file text, p_content text) RETURNS text AS $$
      o=open(args[0],"w")
      o.write(args[1])
      o.close()
      return "ok"
    $$ LANGUAGE PLpythonU;
    

    PS: for safe implementation see this example.


    Preparing

    There are a not-so-obvious procedure to use PLpython extension. Supposing an UBUNTU server:

    1. On SQL check SELECT version().
    2. On terminal check sudo apt install postgresql-plpython listed versions.
    3. Install the correct version, eg. sudo apt install postgresql-plpython-9.6.
    4. Back to SQL do CREATE EXTENSION plpythonu.

    Testing

    The /tmp is default, to create or use other folder, eg. /tmp/sandbox,
    use sudo chown postgres.postgres /tmp/sandbox.

    Suppose the tables of the question's examples. SQL script, repeating some lines:

      DROP TABLE IF EXISTS ttmp; 
      DROP TABLE IF EXISTS xtmp; 
    
      CREATE TABLE ttmp (x text);
      COPY ttmp FROM '/tmp/sandbox/original.xml' ( FORMAT text );
      COPY (SELECT x FROM ttmp) TO '/tmp/sandbox/test1-good.xml' (format TEXT);
    
      CREATE TABLE xtmp (x xml);
      INSERT INTO  xtmp (x) 
         SELECT array_to_string(array_agg(x),E'\n')::xml FROM ttmp
      ;
    
      COPY (select x::text from xtmp) 
      TO '/tmp/sandbox/test2-bad.xml' ( FORMAT text );
    
      SELECT makefile('/tmp/sandbox/test3-good.xml',x::text) FROM xtmp;
    

    The sha1sum *.xml output of my XML original file:

    4947..  original.xml
    4947..  test1-good.xml
    949f..  test2-bad.xml
    4947..  test3-good.xml
    
    0 讨论(0)
  • 2021-01-13 19:08

    COPY isn't designed for this. It's meant to deal with table-structured data, so it can't work without some way of dividing rows and columns; there will always be some characters which COPY FROM interprets as separators, and for which COPY TO will insert some escape sequence if it finds one in your data. This isn't great if you're looking for a general file I/O facility.

    In fact, database servers aren't designed for general file I/O. For one thing, anything which interacts directly with the server's file system will require a superuser role. If at all possible, you should just query the table as usual, and deal with the file I/O on the client side.

    That said, there are a few alternatives:

    • The built-in pg_read_file() function, and pg_file_write() from the adminpack module, provide the most direct interface to the file system, but they're both restricted to the cluster's data directory (and I wouldn't recommend storing random user-created files in there).
    • lo_import() and lo_export() are the only built-in functions I know of which deal directly with file I/O and which have unrestricted access to the server's file system (within the constraints imposed by the host OS), but the Large Object interface is not particularly user-friendly....
    • If you install the untrusted variant of a procedural language like Perl (plperlu) or Python (plpythonu), you can write wrapper functions for that language's native I/O routines.
    • There isn't much you can't accomplish via COPY TO PROGRAM if you're determined enough - for one, you could COPY (SELECT 1) TO PROGRAM 'mv <source_file> <target_file>' to work around the limitations of pg_file_write() - though this blurs the line between SQL and external tools somewhat (and whoever inherits your codebase will likely not be impressed...).
    0 讨论(0)
提交回复
热议问题