How do I copy a CSV file into a Postgres table?

Issue

You want to copy a .csv file from your local machine to a Heroku Postgres table.

Resolution

Run heroku pg:psql -a <application_name>.

Once the Postgres shell opens, run

DATABASE=> \copy table_name FROM csv_file.csv WITH (FORMAT CSV);

(where table_name is the table you wish to copy the data into and csv_file.csv is the path to your local copy of the CSV file)

If an upload data is large, we'd recommend the following:

  • Attempt to run the \copy the data in smaller batches
  • If it's a private tier plan, attempt to run the \copy from a (one-off) dyno to avoid the extra overhead/latency to connect to the database server

For more information about the Postgres COPY command, please read its documentation found here: https://www.postgresql.org/docs/current/sql-copy.html

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support