Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Postgres - Import from CSV via CLI

Having a beast of a time trying to find a solution to trying to import a CSV (local on my machine - don't ask - this is a customer issue and it's the only way I can get data from them at the moment) into my pg database up on heroku. Have tried using COPY to read the file directly from a publicly accessible URL, but I need superuser rights in pg to be able to do that. Tried using cat to pipe the output of the file into stdin, but not sure if I've got the format of that correct (it always seems to complete but with no data imported - says "COPY 0" when it's done).

cat ~/Downloads/localfile.csv | heroku pg:psql -c "COPY testonly FROM STDIN WITH (FORMAT CSV);"

As I'm going to have to do this a few more times over the next couple of months, I'd rather not have to import this locally and then export a backup. Would be great if I could get the COPY option working.

like image 985
dgwebb Avatar asked Feb 26 '18 23:02

dgwebb


1 Answers

You are close, except you need to feed your csv to your local psql. heroku pg:psql does not accept stdin.

Like this

cat ~/Downloads/localfile.csv | \
psql `heroku config:get DATABASE_URL` -c "COPY testonly FROM STDIN WITH (FORMAT CSV);"
like image 148
Stan Mazhara Avatar answered Oct 15 '22 23:10

Stan Mazhara