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

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?

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

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

Related

Partition table data are not restored using pg_restore in PostgreSQL

Need Help !!
While i'm going to restore a partition table using :
pg_restore -U (username) -d (Database) -p (port_number) -t (partition_table) -f (filename)
the result is success but the data in that partition table is not restored, can anyone help me ?
When -t is specified, pg_restore makes no attempt to dump any other database objects that the selected table(s) might depend upon. Therefore, there is no guarantee that the results of a specific-partitioned table dump can be successfully restored
However you can take dump using
pg_dump --host=hostname --username=user -t "child_table_*" --data-only --file=dump_out.sql dbname
and then restore it, This is probably a consequence of the design decision to have partitions visible as individual tables on the SQL level.
please refer:
https://www.postgresql.org/docs/13/app-pgrestore.html
https://www.postgresql.org/docs/11/app-pgdump.html

How can I copy my whole postgres database to another postgres one?

I have two postgres databases on the same server, a live one and another one which I use as a test database.
Periodically I need to copy my live database (both structure of the tables and their data) into the test one, but everything I've found online only copies table by table. Since I often create new tables in my live db I can't do this, otherwise I'd have to update the job every time.
Anybody knows how can I pull the whole live postgres db into the test postgres one?
Thanks!
You want to use pg_dump and pg_restore.
Example usage would be:
$ pg_dump -Fc <database_name> > dumpfile
$ pg_restore <another_database_name> < dumpfile
If you want to do this for all database you might want to consider pg_dumpall
A bit more can be found in https://www.postgresql.org/docs/current/backup-dump.html

How do I export some data my local Postgres db and import it to a remote without deleting all the data from the remote?

I’m using Postgres 9.5 on Mac Sierra. I want to export some table data from my local machine and import that into a Postgres 9.5 database on a Linux machine. Note I don’t want to blow away the data on the Linux machine, only add the my local machine table rows to the rows that already exist on the tables in the Linux environment (and ignore duplicates). So on my local machine, I ran
pg_dump -U myusername --format custom --section data --inserts --file "t1.backup" --table "table1" --table "table2" --table "addresses" "mydb"
However on my remote machine, when I try and import the file, I get the error
myuser#remote-machine:/tmp$ psql db_production < t1.backup
The input is a PostgreSQL custom-format dump.
Use the pg_restore command-line client to restore this dump to a database.
But I don’t want to use pg_restore because I don’t want to erase the existing table data, I simply want to add to it. How can I achieve this?
Use --format plain instead of custom one. The latter is designed to work exclusively with pg_restore. Plain format also allows you to take a look into dumped data with text editor and verify if that's what you want.
However my quick test shows that it's also possible to append data with pg_restore and custom format data-only dump:
pg_restore -d db_production --data-only t1.backup

What causes errors when restoring postgres database?

I'm trying to backup up an entire postgres database and restore it properly, however I am seeing a list of errors when trying to restore the backup.
I am using pg_dump to create a backup sql file. (I have a .pgpass file for password)
sudo -u postgres pg_dump -d db-w > backup.sql
When I try to restore the database with:
sudo -u postgres psql db < backup.sql
I get a list of errors like:
ERROR: duplicate key value violates unique constraint
ERROR: multiple primary keys for table
ERROR: relation <relation> already exists
ERROR: trigger <trigger> for relation <relation> already exist
I haven't made any changes to the database. I simply performed a backup and restore the backup right after.
What am I doing wrong?
your restoring on an existing database, if you want and sure to replace database with the backup you can use option --clean and --create
-c, --clean
Clean (drop) database objects before recreating them. (This might
generate some harmless error messages, if any objects were not
present in the destination database.)
-C, --create
Create the database before restoring into it. If --clean is also
specified, drop and recreate the target database before connecting
to it.

Can I restore just one schema from a pg_dump of the entire database?

I have backups of my postgres database - the entire database instance in a single nightly backup. Is it possible to restore just one of the database from within that backup? Or if I want to access individual databases (for migration or restoring), do I need to change my database backup scheme to do individual dumps?
You can access individual databases from a full database cluster dump in text format (pg_dumpall) by using some text processing like this:
awk '/^\\connect database_name/ {flag=1;print;next}
/^\\connect/ {flag=0}
flag { print }' \
< all_databases.sql \
> database_name.sql
This would get from pg_dumpall file everything between "\connect database_name" and next "\connect". But it is not very efficient.
But I'd recommend dumping every database separately like this:
# Dumping global data (for example roles)
pg_dumpall -g > /var/lib/pgsql/backups/globals.sql
#Dumping indidual databases in tar (uncompressed binary) format
for dbname in
`
psql -qXtc "
select datname from pg_catalog.pg_database
where datname<>'template0'" template1
`
do
pg_dump -b -F t "$dbname" > "/var/lib/pgsql/backups/$dbname.dump"
done
I'm assuming you mean "Can I restore just one database from a pg_dumpall of the entire database cluster?"
Yes, if your backup is "an archive created by pg_dump in one of the non-plain-text formats." - use the "--schema=" option to pg_restore.
On the other hand I'm not sure you're using the correct terminology here - you refer to a DB cluster as "entire database instance"; in the explanation you ask about "database" but in the title you wrote "schema", etc.
Edit: Now, after some deliberation, I believe you have a cluster backup, created with "pg_dumpall". "pg_dumpall" creates only plain-text (SQL-commands) backups. In that case it's not possible to restore only one database (or only one schema from a database).
Then yes, you need to change your backup procedure to backup individual databases using non-plain-text format (--format=custom is the recommended one).
Actually the backup procedure I use on my DB servers does just that.