Sorry if this is to generic but am new to PostgreSQL. I have an existing product that uses another database. I am looking into converting to PostgreSQL. Therefore, I will need to write a conversion program.
The existing db uses the typical integer autoinc fields for the primary key for each table. I would like to do the same in PostgreSQL. Is there a way create my PostgreSQL tables with an integer ID field, import the data and then create my sequences on the ID field where the nextval is some arbitrary integer.
Thanks
Converting Database
You can use SERIAL data type. PostgreSQL automatically creates a sequence for such fields. Use the following statement to set the last value for a sequence:
Code: Select all
SELECT pg_catalog.setval('seq_name', LastVal, false)
-
- Posts: 5
- Joined: Tue 12 Jan 2010 01:03
Thank you very much. Just to make sure that I understand:
I create the column as a serial.
When importing the rows from the other db I just manually specify the values with the id numbers from the old db.
Then when I have all the data imported, I calculate the next id number and set lastval of the sequence with the commend you mentioned.
I create the column as a serial.
When importing the rows from the other db I just manually specify the values with the id numbers from the old db.
Then when I have all the data imported, I calculate the next id number and set lastval of the sequence with the commend you mentioned.