简体   繁体   English

如何将 .csv 文件导入 Hasura PostgreSQL 数据库?

[英]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.我有一个 .csv 文件中的数据,我想将其导入到我的 Hasura 集群的 PostgreSQL 数据库实例中。 What's the best way to do this?做到这一点的最佳方法是什么?

Create table_name with the appropriate schema to absorb your CSV data;使用适当的架构创建table_name以吸收您的 CSV 数据; use psql to stream data on to postgres.使用 psql 将数据流式传输到 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您将在表table_name拥有来自 CSV 文件的数据

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.在 Heroku 中部署 Hasura 时,我们可以通过从 Heroku 资源仪表板访问 Postgres 附加组件来获取 Postgres 数据库的临时凭证。 Then you can access the database directly using the url provided on the settings tab.然后您可以使用设置选项卡上提供的 url 直接访问数据库。

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:然后在 Postgres 控制台中,您可以执行以下操作:

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

The above for a tab delimited file downloaded from Geonames.org.以上是从 Geonames.org 下载的制表符分隔文件。 Note: I deleted the comment lines before input.注意:我在输入之前删除了注释行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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