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?
Restoring the data from pg_dump doesn't overwrite the data but it appends the data to the original database.
Heads up: pg_restore --clean keeps existing tables Note that this only removes data from tables that are part of the dump and will not remove any extra tables. You need to do that yourself.
pg_dump is a utility for backing up a PostgreSQL database. It makes consistent backups even if the database is being used concurrently. pg_dump does not block other users accessing the database (readers or writers). pg_dump only dumps a single database.
sql , which uses the plain or SQL format, the pg_dump command does not store any file anywhere. It just sends the output to STDOUT , which is usually your screen, and it's done. But in your command, you also told your shell (Terminal, Command prompt, whatever) to redirect STDOUT to a file.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With