I have a function based this post by Sean Huber in order to get the content of a file:
CREATE FUNCTION file_read(file text) RETURNS void AS $$ DECLARE content text; tmp text; BEGIN file := quote_literal(file); tmp := 'tmp_table';EXECUTE 'CREATE TEMP TABLE ' || tmp || ' (content text)'; EXECUTE 'COPY ' || tmp || ' FROM ' || file; EXECUTE 'SELECT content FROM ' || tmp INTO content; Do some more stuff here EXECUTE 'DROP TABLE ' || tmp;
END; $$ LANGUAGE plpgsql VOLATILE;
I'm not really happy with this as it is doing so much more work than necessary. I'd prefer not to create/drop relations cause all I really want to do is run Postgres' JSON functions against the content of some .json file. Does anyone know of a better way to do this without using psql?