I am dumping a large Postgres table like this:
pg_dump -h myserver -U mt_user --table=mytable -Fc -Z 9 --file mytable.dump mydb
The above creates a mytable.dump
file. I now want to restore this dump into a new table called mytable_restored
.
How can I use the pg_restore
command to do this?
Use the pg_restore command-line client to restore this dump to a database. I don't really understand the -C option. Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before connecting to it.
The pg_dump is a tool for backing up a database into the file. We can restore it later into another database. To back up the database into a file run, -W forces to prompt for a password while logging to the database. -F specifies the format of the dump file created, here t refers tar file.
Don't do SQL backups if you need single table restore, etc. Use pg_dump 's -Fc option - the "custom" format. This can be restored using pg_restore. Selective restore is possible, as are all sorts of other handy features. pg_restore can convert a custom-format dump into an SQL dump later if you need it.
There are several other options, including -Fc to create a backup with a custom format that can be used with the pg_restore command. The pg_dump command provides several options, including the ability to drop and create the database. If you need to back up all the databases, use the pg_dumpall command, however, it’s not as flexible.
There is no pg_restore
option to rename tables.
I would do it like this:
-- create a table that is defined like the original
CREATE TABLE mytable_restored (LIKE mytable INCLUDING ALL);
-- copy the table contents
COPY mytable TO '/tmp/mytable.dmp';
COPY mytable_restored FROM '/tmp/mytable.dmp';
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