Search code examples
sqlpostgresqlpgadmin-4

PostgresSQL: backup table as UPDATE statements


I've made a mistake. I've updated a table without where... Fortunatelly I have a backup. I cant delete the table and restore it, because of foreign key restrictions... I want to save the original table as UPDATE statements to restore the values, ¿how can I do that?


Solution

    1. Import the backed up table with another name (ie. backup).
    2. Update the original table with an UPDATE JOIN.

    Example:

        UPDATE mytable
        SET mytable.c1 = backup.c1, mytable.c2 = backup.c2,
        FROM backup
        WHERE mytable.id = backup.id;