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

送分小仙女□ 提交于 2019-12-20 05:27:31

问题


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.


回答1:


You can RAISE NOTICE or DEBUG messages in a plpgsql function or a DO statement which are written to the DB log file.




回答2:


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(text) RETURNS text AS '\n
o=open("/path/to/file")
o.write(args[0])
o.close()
return "ok"
' LANGUAgE plpythonu;

Regards Tino



来源:https://stackoverflow.com/questions/39812722/how-can-i-write-to-a-file-on-disk-from-pl-pgsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!