How to allow permission to access CSV file using postgres in Ubuntu

Posted on Sep 23, 2022

Question

I am using the following command :

copy (select so.name,
      so.date_order,
      sol.name,
      sol.product_Id,
      sol.product_uom_qty , 
      ai.number, 
      ai.date_invoice , 
      so.amount_total , 
      so.amount_tax 
      from sale_order so , 
      sale_order_line sol , 
      account_invoice ai 
      where so.id = sol.order_id 
      and so.name = ai.origin 
      and ai.state='open') 

to ‘/home/ekodev/Documents/test1.csv’ delimiter ‘,’ csv header;

However, it gives the following error :

********** Error **********

ERROR: must be superuser to COPY to or from a file SQL state: 42501 Hint: Anyone can COPY to stdout or from stdin. psql’s \copy command also works for anyone.

Even when I changed the permission ekodev@partner:~/Documents$ sudo chmod a+rwX /home/ekodev/ /home/ekodev/Documents/ /home/ekodev/Documents/test1.csv

It still does not work for me.

Does anybody know what the problem is?

Answer

This solution worked for me using \copy. ALTER did not as that also required admin privileges.

psql -h <host> -U <user> -d <dbname> -c "\copy <table_name> FROM '<path to csvfile/file.csv>' with (format csv,header true, delimiter ',');"