Re: Strategy for Primary Key Generation When Populating Table

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Strategy for Primary Key Generation When Populating Table
Date: 2012-02-09 17:18:44
Message-ID: CAOR=d=0wV+OA+mTM1s2BZ-8Rni6wkM0imw_qxH6NWe1G9_==tw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Feb 9, 2012 at 9:49 AM, Rich Shepard <rshepard(at)appl-ecosys(dot)com> wrote:
>  I have a lot of data currently in .pdf files. I can extract the relevant
> data to plain text and format it to create a large text file of "INSERT INTO
> ..." rows. I need a unique ID for each row and there are no columns that
> would make a natural key so the serial data type would be appropriate.
>
>  When I prepare the text file I can start each row with the delimiter (',')
> to indicate there's a table column preceding. If I define the primary key
> as serial type on that first position in the file, will postgres
> automagically fill it in as each row is read into the table?
>
>  If not, or if there's a better way of approaching this task, please clue
> me in to that.

If you format your copy statement with a column list that leaves out
the serial key the db will insert that for you.

file: /tmp/input.sql
copy test (i1) from stdin;
10
20
30
40
50
\.

create table test (id serial primary key, i1 int);
\i /tmp/input.sql
select * from test
id | i1
----+----
1 | 10
2 | 20
3 | 30
4 | 40
5 | 50
(5 rows)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gary Chambers 2012-02-09 18:55:30 Re: Warning: you don't own a lock of type ExclusiveLock
Previous Message Andy Colson 2012-02-09 17:10:18 Re: Strategy for Primary Key Generation When Populating Table