I have a table in PG 9.6 with unique key constraint on it. I took the backup using pg_dump and restore it in PG12. While restoring I got error for the table mentioned with unique key constraint failed. After checked in PG 9.6, Column contains few duplicates.
Here Question is
How is it possible for containing duplicates in column with unique key constraint ?
Also, how to fix this error in PG12 without deleting duplicates (PG 9.6 contains duplicates)?
Related
Precondition:
I am using Liquibase with SQL scripts in my app. I started testing with Oracle DB, but now I need to switch to PostgreSQL DB.
Problem:
When I added constraints, I didn't add the names of the constraints.
Liquibase changelog contains a script for dropping unique and primary key constraints:
alter table SCENARIO drop primary key/
alter table SCENARIO drop unique (OWNER_ID)/
This syntax doesn't sync with PostgreSQL
Could you give some advice, on how to resolve this problem?
Screenshots:
We are using postgres version 9.3 in our production databases.
We found that some of them contain null values in a column that has a PK constraint. In a given table, there were 53 null-value records in a column with PK. We found the same problem with some columns with constraint NOT NULL. We found the inconsistency when we tried to update an column of the table that has this problem and received an error message regarding the constraints. How can data be inconsistent if constraints exist? Is this a postgres bug or some kind of database corruption?
I imported all tables from MySQL to PostgreSQL but now I have problems with id's.
The way I converted my MySQL DB was simple exported DB and copied all "INSERTS" with edited syntax, import was successful because I can see all
the data correct.
SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "elements_pkey"
DETAIL: Key (id)=(1) already exists.
Is there any way to fix issues with id's?
It works after reset the sequence.
SELECT setval('my_sequence_name', (SELECT max(id) FROM my_table));
As a database developer, I experienced this notice when I tried to make a data-only dump a PostgreSQL(10.1) database 'tlesson'.
Notice =>
pg_dump: NOTICE: there are circular foreign-key constraints on this table:
pg_dump: members
Dump command =>
$ pg_dump -U postgres -d translesson -a
A 'tlesson' table 'members' constraint =>
ALTER TABLE ONLY members
ADD CONSTRAINT friend_fk FOREIGN KEY (friend_id) REFERENCES members(member_id);
That is, 'friend_id' column refers own table's primary key as the foreign-key.
Should I drop the 'friend_fk' constraint to remove the notice I'm having?
If you always drop the entire database then this isn't a problem, because the generated SQL (or pg_restore) will enable (create) foreign keys only after all the data was loaded, so there is no problem in that case.
However if you only dump a single table without the FKs then, importing is only going to work if you manually drop the FK before restoring, then re-create it afterwards.
The reason is that it's nearly impossible to generate INSERT statements in the correct order if you have circular references
I used the commands pg_dump and psql to backup my production DB and restore it into my development server.
Now when I try to simply insert a new record to one of my tables I get the following error message:
ERROR: duplicate key value violates unique constraint
"communication_methods_pkey" DETAIL: Key (id)=(13) already exists.
How come that the id is already in use? I need to update something in order to have the id increment counter back on the right track?
It sounds like the sequences used to do the primary key for each table are not on the correct value. It is interesting that pg_dump did not include a sequence setval at the end of it (I believe it is supposed to).
Postgres recommends the following process to correct sequences: https://wiki.postgresql.org/wiki/Fixing_Sequences
Essentially, it takes you through identifying all your sequences and creating a sql script to run to set them to 1 more than your inserted value's ids.