How can I write to a file on disk from PL/pgSQL?

前端 未结 3 1407
盖世英雄少女心
盖世英雄少女心 2021-01-25 08:37

I would like to do the equivalent of c or php fopen() and fwrite(). I am not trying to dump a table to disk. I am trying to do some debug logging during development.

3条回答
  •  春和景丽
    2021-01-25 09:07

    PostgreSQL 12 + Python 3

        CREATE OR REPLACE FUNCTION "public"."makefile"("path" text, "content" text)
          RETURNS "pg_catalog"."text" AS $BODY$
            import os
            import stat
            o=open(path,'w')
            o.write(content)
    
            # stat.S_IRUSR Owner has read permission.
            # stat.S_IWUSR Owner has write permission.
            # stat.S_IRGRP Group has read permission.
            # stat.S_IWGRP Group has write permission.
            # stat.S_IROTH Others have read permission.
            os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
            o.close()
            return 'ok'
        $BODY$
          LANGUAGE plpython3u VOLATILE
    

提交回复
热议问题