How to Create Table from CSV

Lists: pgsql-general
From: ray <ray(at)aarden(dot)us>
To: pgsql-general(at)postgresql(dot)org
Subject: How to Create Table from CSV
Date: 2011-03-06 18:43:22
Message-ID: b1361c5a-d8a5-4eb7-a284-20c5e52a0e26@j35g2000prb.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I would like to create a table from a CSV file (the first line is
headers which I want to use as column names) saved from Excel. I have
a new database which I have been able to create tables from a
tutorial. But I haven’t been able to produce this new table. The
following are my attempts:

CREATE TABLE Equpment_List_Sheet2 FROM 'Equipment List, reference
r1_Sheet2.csv' WITH DELIMITER ',' NULL '' CSV HEADER;
ERROR: syntax error at or near "FROM"
LINE 1: CREATE TABLE Equpment_List_Sheet2 FROM 'Equipment List,
refe...

^
********** Error **********
ERROR: syntax error at or near "FROM"
SQL state: 42601
Character: 35
____________
copy TABLE Equpment_List_Sheet2 FROM 'Equipment List, reference
r1_Sheet2.csv' WITH DELIMITER ',' NULL '' CSV HEADER;
ERROR: syntax error at or near "TABLE"
LINE 1: copy TABLE Equpment_List_Sheet2 FROM 'Equipment List,
refere...
^
********** Error **********
ERROR: syntax error at or near "TABLE"
SQL state: 42601
Character: 6
______________
copy Equpment_List_Sheet2 FROM 'Equipment List, reference
r1_Sheet2.csv' WITH DELIMITER ',' NULL '' CSV HEADER;
ERROR: relation "equpment_list_sheet2" does not exist
********** Error **********
ERROR: relation "equpment_list_sheet2" does not exist
SQL state: 42P01
_______________

Thanks for any help,
ray


From: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
To: ray <ray(at)aarden(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to Create Table from CSV
Date: 2011-03-06 18:59:06
Message-ID: C74E3956-4336-4AEF-A151-E733D30C7582@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 7, 2011, at 12:13 AM, ray wrote:

>
> CREATE TABLE Equpment_List_Sheet2 FROM 'Equipment List, reference
> r1_Sheet2.csv' WITH DELIMITER ',' NULL '' CSV HEADER;
> ERROR: syntax error at or near "FROM"
> LINE 1: CREATE TABLE Equpment_List_Sheet2 FROM 'Equipment List,
> refe...
>
> ^
> ********** Error **********
> ERROR: syntax error at or near "FROM"
> SQL state: 42601
> Character: 35
> ____________
> copy TABLE Equpment_List_Sheet2 FROM 'Equipment List, reference
> r1_Sheet2.csv' WITH DELIMITER ',' NULL '' CSV HEADER;
> ERROR: syntax error at or near "TABLE"
> LINE 1: copy TABLE Equpment_List_Sheet2 FROM 'Equipment List,
> refere...
> ^
> ********** Error **********
> ERROR: syntax error at or near "TABLE"
> SQL state: 42601
> Character: 6

You can't create a table using CSV. However try following:
1. Create structure of table (Parse the column using some script or manually)
2. Import data using COPY Command.

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor(dot)kumar(at)enterprisedb(dot)com
Blog:http://vibhork.blogspot.com


From: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to Create Table from CSV
Date: 2011-03-06 19:20:09
Message-ID: alpine.LNX.2.00.1103061117030.23837@salmo.appl-ecosys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sun, 6 Mar 2011, ray wrote:

> I would like to create a table from a CSV file (the first line is
> headers which I want to use as column names) saved from Excel. I have
> a new database which I have been able to create tables from a
> tutorial. But I haven?t been able to produce this new table. The
> following are my attempts:

As mentioned, write the table structure to a file using the postgres DDL.
That is, 'CREATE TABLE <name> (
column1...
);'

Then using psql you can '\copy to <tablename> from <filename> with delimiter
as ',' null as '' CSV' (without the quotes and with appropriate delimiter
and null values. The backslash is needed to make it work.

Rich


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: ray <ray(at)aarden(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to Create Table from CSV
Date: 2011-03-06 21:18:48
Message-ID: m2d3m4eymf.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

ray <ray(at)aarden(dot)us> writes:
> I would like to create a table from a CSV file (the first line is
> headers which I want to use as column names) saved from Excel. I have

You have to manually create the table and its columns, as other said.
The tricky part that is hard (or impossible) to automate is deciding
which data type to use for each column.

Once you've done that, actually importing the data is a matter of using
the COPY command or the pgloader tool.

In PostgreSQL 9.1 you will be able to use CREATE FOREIGN TABLE to
achieve that in one step, see:

CREATE FOREIGN TABLE

http://developer.postgresql.org/pgdocs/postgres/ddl-foreign-data.html
http://developer.postgresql.org/pgdocs/postgres/sql-createforeigndatawrapper.html
http://developer.postgresql.org/pgdocs/postgres/file-fdw.html

Regards,
--
Dimitri Fontaine
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support


From: "ray joseph" <ray(at)aarden(dot)us>
To: "'Dimitri Fontaine'" <dimitri(at)2ndQuadrant(dot)fr>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to Create Table from CSV
Date: 2011-03-07 01:38:30
Message-ID: 0947F8B1BA924E60A883FB790AF1E5F8@corp.kbr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I appreciate all the information

Thank you,
ray

-----Original Message-----
From: Dimitri Fontaine [mailto:dimitri(at)2ndQuadrant(dot)fr]
Sent: Sunday, March 06, 2011 3:19 PM
To: ray
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] How to Create Table from CSV

ray <ray(at)aarden(dot)us> writes:
> I would like to create a table from a CSV file (the first line is
> headers which I want to use as column names) saved from Excel. I have

You have to manually create the table and its columns, as other said.
The tricky part that is hard (or impossible) to automate is deciding
which data type to use for each column.

Once you've done that, actually importing the data is a matter of using
the COPY command or the pgloader tool.

In PostgreSQL 9.1 you will be able to use CREATE FOREIGN TABLE to
achieve that in one step, see:

CREATE FOREIGN TABLE

http://developer.postgresql.org/pgdocs/postgres/ddl-foreign-data.html

http://developer.postgresql.org/pgdocs/postgres/sql-createforeigndatawrapper
.html
http://developer.postgresql.org/pgdocs/postgres/file-fdw.html

Regards,
--
Dimitri Fontaine
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support