Question

we can use

copy (select * from mytbl) to 'D:/products.csv' with csv header 

to import data in mytbl to local disk D

so is it possible to use the same method to upload the file directly into a FTP-Server ?

i tried like this

copy (select * from mytbl) to 'ftp://usrname:mypasswrd@ftp.drivehq.com/masters/3/product/products.csv' with csv header 

but got this error

ERROR: relative path not allowed for COPY to file
SQL state: 42602

using PostgreSQL 9.2

Was it helpful?

Solution

PostgreSQL does not support any source/destination for COPY other than a file or stdin/stdout.

What you can do is COPY to stdout and pipe that to a program that writes the data to the ftp dir. psql's \copy is useful for this:

psql -c "\copy mytable to stdout with (format csv, header)" | ncftpput -c my.ftp.host /path/on/host

You can use any tool that accepts the input data on a pipe to write to the remote ftp file; ncftpput is just one option.

A future PostgreSQL version may add support for invoking COPY with a pipe, e.g. COPY ... TO '|/some/command', but there are serious security concerns with running programs under the PostgreSQL user that would make this a superuser-only operation and of questionable safety even then. It's much safer to run the program client-side, and psql is ideal for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top