Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore output from pg_dump into a new table name

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?

like image 901
Anthony Avatar asked May 20 '19 20:05

Anthony


People also ask

How do I restore a dump to a database using PG_restore?

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.

What is PG_dump command in PostgreSQL?

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.

How do I restore a table from a dump?

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.

How do I create a backup using the PG_dump command?

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.


1 Answers

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';
like image 94
Laurenz Albe Avatar answered Sep 18 '22 11:09

Laurenz Albe