我可以从带有标题的 csv 文件在 PostgreSQL 中自动创建表吗? [英] Can I automatically create a table in PostgreSQL from a csv file with headers?
问题描述
我在 OS X 10.6.8 上运行 PostgreSQL 9.2.6.我想将带有列标题的 CSV 文件中的数据导入数据库.我可以使用 COPY
语句来做到这一点,但前提是我首先手动创建一个表,该表为 CSV 文件中的每一列都有一个列.有没有办法根据 CSV 文件中的标题自动创建这个表?
I'm running PostgreSQL 9.2.6 on OS X 10.6.8. I would like to import data from a CSV file with column headers into a database. I can do this with the COPY
statement, but only if I first manually create a table with a column for each column in the CSV file. Is there any way to automatically create this table based on the headers in the CSV file?
每 这个问题我试过了
COPY test FROM '/path/to/test.csv' CSV HEADER;
但我刚刚收到此错误:
错误:关系test"不存在
如果我首先创建一个没有列的表:
And if I first create a table with no columns:
CREATE TABLE test();
我明白了:
错误:最后一个预期列之后的额外数据
我在 PostgreSQL COPY 文档中找不到任何内容 关于自动创建表.有没有其他方法可以从带有标题的 CSV 文件自动创建表格?
I can't find anything in the PostgreSQL COPY documentation about automatically creating a table. Is there some other way to automatically create a table from a CSV file with headers?
推荐答案
您在 COPY
文档,因为 COPY 无法为您创建表格.
您需要先执行此操作,然后才能COPY
到它.
You can't find anything in the COPY
documentation, because COPY cannot create a table for you.
You need to do that before you can COPY
to it.
这篇关于我可以从带有标题的 csv 文件在 PostgreSQL 中自动创建表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!