简体   繁体   中英

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

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM