I am porting a MySQL database to PostgreSQL. So far I have managed to rebuild all the tables and columns and import the data. However, the auto incremented, unique id
fields have not been converted properly. I used the pgAdmin GUI to correct one column in a table. I think I am probably not the first one converting from MySQL to Postgres so there probably is an robust and tested method to do this as an automated task. I would appreciate it if someone could provide such a small script.
The main reason, however, why I would like a tested solution for this is because I ran into an issue after adding a sequence and setting the default value for the column in question. The current value of the sequence was set correctly to 2069
, the value of the last table entry. When I ran my program and added a row I got an exception which said that the unique value 2069
was already assigned. When I ran the program again it created the new row but the auto incremented id was 2071
. It skipped 2070
.
So now there are 3 possible sources of error:
- Bug in PostgreSQL 9.5
- Bug in the Java JDBC connector
- I made a mistake with the auto increment field and sequence.
By using a tested solution I could eliminate the last possibility.