Restoring the data from pg_dump doesn't overwrite the data but it appends the data to the original database

21,840

Solution 1

The default format fo pg_dump is plain, so it creates a COPY statement. Hence when you psql backup.sql you just run those copy over existing data. To rewrite data, you should either drop tables first or pg_dump -F c and pg_restore -c.

Warning - in both cases it will destroy old data (this seems what you want though)

-c --clean Clean (drop) database objects before recreating them. (Unless --if-exists is used, this might generate some harmless error messages, if any objects were not present in the destination database.)

As @Craig Ringer suggests, drop/recreate from backup would be much easier and cleaner. To drop database you run DROP DATABASE au - note that there should be no connected users to success. Then you have to create db: CREATE DATABASE au and run psql -f backup.sql -d au

Solution 2

Take the dump with -c option: pg_dump -c database_name > backup.sql. See pg_dump docs.

Share:
21,840
SonamGupta
Author by

SonamGupta

I know what I am! One day ,I 'll tell you what I am when I have sufficient facts n statistics which proves who I am! Nevertheless , the people who loves me n the people whom I love knows me better :)

Updated on July 05, 2022

Comments

  • SonamGupta
    SonamGupta almost 2 years

    I am taking the dump of postgres database using "pg_dump database_name > backup.sql". Later on I am doing some modifications in the original database(database_name) and then I am restoring the data from the backup file(backup.sql). But the result is that, the database doesn't gets restored to the original state, instead it adds the original data to the modified data(modified + original).I just want it to restore to the original state, shall i delete all the data from the database before restoring it from the backup file, since it gives the original state of the database. Or is there any other way to do this?

  • Craig Ringer
    Craig Ringer about 7 years
    Usually better to drop and re-create the database. That way you know you dropped data types, schemas, and anything else.
  • Vao Tsun
    Vao Tsun about 7 years
    yes - OP did not show us dbname, so either he hides it, or have in env, or works in postgres. I did not dare to offer him dropping db :)
  • SonamGupta
    SonamGupta about 7 years
    So what things should I delete from the database before restoring? Should I delete the "tables" , "views", "schemas" or else what? And if I have to drop all the tables from the database, then how can I do it in a single query without writing the name of all the tables, since it is a huge database containing 107 tables?
  • Vao Tsun
    Vao Tsun about 7 years
    please quote exact command you use to make a backup and mention the dbname you use. the easiest and clean way - drop db and recreate it from backup
  • SonamGupta
    SonamGupta about 7 years
    "filename=bc_"$(date +\%s)".sql
  • SonamGupta
    SonamGupta about 7 years
    "filename=bc_"$(date +\%s)".sql " and "pg_dump au > $filename" is what I am using write now to create the backup file, the name of the database is "au". And for taking the backup, I am using "psql au < bc_1493202909.sql " where "bc_1493202909.sql " is the backup file which was created. If I use drop db , will it not drop the whole database? N for restoring we have will have to create one new database, isn't it?