5

I have data in a .csv file that I want to import into my Hasura cluster's PostgreSQL database instance. What's the best way to do this?

iamnat
  • 3,553
  • 17
  • 34
Kartikey
  • 51
  • 2

2 Answers2

5

Create table_name with the appropriate schema to absorb your CSV data; use psql to stream data on to postgres. Execute this command:

$ psql <postgres-url> -d <database-name> -U <user-name> -c \
  "copy table_name from STDIN with delimiter as ',';" \
  < /path/to/file.csv

You will have the data from CSV file inside table table_name

Shahidh
  • 2,002
  • 1
  • 12
  • 16
  • 1
    If you don't have psql installed locally, you can also use ms cp and ms exec commands: `$ hasura ms cp /path/to/file.csv hasura/postgres:/data.csv` and `$ hasura ms exec postgres -n hasura -- psql -U admin -d hasuradb -c "copy table_name from STDIN with delimiter as ',';" < /data.csv` – Shahidh Jan 17 '18 at 10:02
  • Do you have a reference for the `hasura microservice` command? I can't see it in the [Hasura CLI docs](https://docs.hasura.io/1.0/graphql/manual/hasura-cli/index.html) – Robin Métral Feb 16 '20 at 17:13
0

Adding my answer here for reference. When deploying Hasura in Heroku we can get temporary credentials for the Postgres database by accessing the Postgres add-on from the Heroku resources dashboard. Then you can access the database directly using the url provided on the settings tab.

psql 'postgres://UUUUUU:PPPPP@ec2-54-247-72-30.eu-west-1.compute.amazonaws.com:5432/DBNAME'

Then in the Postgres console you can do something like:

\copy countryinfo from 'countryinfo.csv' with delimiter as E'\t';

The above for a tab delimited file downloaded from Geonames.org. Note: I deleted the comment lines before input.

Julian
  • 1,450
  • 11
  • 25