我想做相当于c或php的fopen()和fwrite()。我不是想将表转储到磁盘上。我正在尝试在开发期间进行一些调试日志记录。
答案 0 :(得分:2)
您可以在postgres函数中使用plpythonu f.open(),f.write(),f.close()来写入文件。
需要安装语言扩展名。
https://www.postgresql.org/docs/8.3/static/plpython.html
例如plpythonu
CREATE FUNCTION makefile(text) RETURNS text AS '\n
o=open("/path/to/file")
o.write(args[0])
o.close()
return "ok"
' LANGUAgE plpythonu;
此致 蒂诺
答案 1 :(得分:1)
您可以在plpgsql函数中发送RAISE NOTICE
或DEBUG
条消息,或者DO
statement写入数据库日志文件。
答案 2 :(得分:0)
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