Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql function load csv WITHOUT SUPERUSER

Does any way to load csv into postgresql table via COPY WITHOUT SUPERUSER privileges exist?

like image 289
h3llca7 Avatar asked Sep 12 '25 00:09

h3llca7


2 Answers

I managed to make it work like this:

cat myfile.csv | psql -d mydb -c "COPY landing_tbl(field01, field02...) FROM STDIN CSV;"
like image 144
Daniel Burgess Avatar answered Sep 13 '25 19:09

Daniel Burgess


You can either use

COPY tabname FROM 'filename'

which requires the superuser privilege or

COPY tabname FROM STDIN

which doesn't require the superuser privilege.

PostgreSQL requires the superuser privilege to access files on the database server for security reasons.

One workaround is to write a database function with SECURITY DEFINER owned by a superuser that does the COPY for you. Then everybody with EXECUTE rights on the function (by default everybody, so change that) can perform the operation.

like image 20
Laurenz Albe Avatar answered Sep 13 '25 20:09

Laurenz Albe