How do I import a .csv file into my Hasura PostgreSQL database?
Asked Answered
B

3

7

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?

Boarder answered 19/11, 2017 at 18:2 Comment(1)
Best way is to post your code. And the resulting error message. #periodGaribull
B
6

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

Blameless answered 20/11, 2017 at 6:34 Comment(2)
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.csvBlameless
Do you have a reference for the hasura microservice command? I can't see it in the Hasura CLI docsAnny
O
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:[email protected]: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.

Ocotillo answered 10/4, 2020 at 16:8 Comment(0)
S
0

You can use pgAdmin to simplify the task. Connect to your postgres instance, then go to Import/Export Data under Tools menu after selecting the desired table from the left sidebar.

Select the .csv file and click on "Ok"

You should now have all your data imported successfully.

Stomy answered 28/7, 2023 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.