From: | Ben Madin <lists(at)remoteinformation(dot)com(dot)au> |
---|---|
To: | adebarros <adebarros(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: COPY from CSV, passing in default value? |
Date: | 2012-05-17 01:21:38 |
Message-ID: | 56546D15-5E0E-45A2-93BB-E00397BAE6C9@remoteinformation.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Does creating a table with a default not work?
CREATE TABLE salaries (
Town varchar(30),
County varchar(30) NOT NULL DEFAULT 'Australia',
Supervisor varchar(30),
StartDate date,
Salary int,
Benefits int
);
You might also want an auto-incrementing primary key, especially if you are importing data so you can delete any duplicates…
CREATE TABLE salaries (
id serial unique PRIMARY KEY,
Town varchar(30),
County varchar(30) NOT NULL DEFAULT 'Australia',
Supervisor varchar(30),
StartDate date,
Salary int,
Benefits int
);
An alternative that becomes simpler for importing repeatedly is to create a temporary table with the same column names as your csv file, but all the data types varchar. Import the csv (which is now easy even if there are '' in the salary field, which are not int) and then insert (with appropriate casting) the results from the temp table into the real table.
cheers
Ben
On 15/05/2012, at 1:31 AM, adebarros wrote:
> Assuming I have a table structured like so:
>
> CREATE TABLE salaries (
> Town varchar(30),
> County varchar(30),
> Supervisor varchar(30),
> StartDate date,
> Salary int,
> Benefits int
> );
>
> If I have a CSV with only three of those fields, I can import like this:
>
> COPY salaries (Town, Supervisor, Salary)
> FROM 'C:\salaries.csv'
> WITH (FORMAT CSV);
>
> However, what if I wanted to assign a default value during import to
> populate the County field? In my dreams it would be something like this
> (which does not work):
>
> COPY salaries (Town, 'County Name', Supervisor, Salary)
> FROM 'C:\salaries.csv'
> WITH (FORMAT CSV);
>
> Any ideas?
>
> Thanks.
>
>
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/COPY-from-CSV-passing-in-default-value-tp5708672.html
> Sent from the PostgreSQL - general mailing list archive at Nabble.com.
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
From | Date | Subject | |
---|---|---|---|
Next Message | adebarros | 2012-05-17 01:35:28 | Re: COPY from CSV, passing in default value? |
Previous Message | Basil Bourque | 2012-05-16 22:05:40 | Re: Cannot find installers for 9.2 Beta |