我可以从带有头的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();
我得到:
错误: / code>
ERROR: extra data after last expected column
我在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
。
这篇关于我可以从带有头的csv文件在PostgreSQL中自动创建一个表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!