vendredi 31 juillet 2015

PoestgreSQL - Import CSV with PSQL Function as Non-superuser

I am using PG Admin and would like to get around the cumbersome method of importing CSVs manually. I've come across the COPY command, similar to this:

COPY table_nameFROM '\\server\mypath\myfile.csv'(FORMAT 'csv', DELIMITER ',', HEADER)

I'm not a superuser, so I get the following error:

ERROR:  must be superuser to COPY to or from a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.

Ideally, I would like to create a function that I can run when needed. It would be great if the function could CREATE TABLE on the fly and import all fields as text:

select csvimporter('table_name','\\server\mypath\myfile.csv',',','HEADER')

I am still fairly new to functions. Has anyone had experience using functions like this while not being a superuser? Any help/tips are much appreciated.

Thanks!

Aucun commentaire:

Enregistrer un commentaire