1

The Postgrex documentation gives this excellent example of how to transfer data:

Postgrex.transaction(pid, fn(conn) ->
  query = Postgrex.prepare!(conn, "", "COPY posts TO STDOUT")
  stream = Postgrex.stream(conn, query, [])
  result_to_iodata = fn(%Postgrex.Result{rows: rows}) -> rows end
  Enum.into(stream, File.stream!("posts"), result_to_iodata)
end)

Postgrex.transaction(pid, fn(conn) ->
  stream = Postgrex.stream(conn, "COPY posts FROM STDIN", [])
  Enum.into(File.stream!("posts"), stream)
end)

However, this will not work on Redshift because Redshift doesn't support COPY TO STDOUT. If I try it from Postgrex I get a syntax error, same for trying it from psql. According to Redshift, 'COPY TO file from Xen-tables not supported'.

So I switched my COPY TO STDOUT to a SELECT, but now I don't know how to load it in the second part. If I keep it as COPY FROM STDIN, it just loads all of the results into one row.

Any suggestions?

Alex V
  • 3,416
  • 2
  • 33
  • 52

0 Answers0