how to delete a table before importing it on pg_dump?

14k views Asked by At

I'm trying to insert into another database table that I exported using pg_dump:

pg_dump -U postgres --column-inserts --data-only -t table3 -t table2 -t table 1  > c:/file database

The problem is that the tables exist in the other db and I'm getting many primary key errors.

Any other ways to update the tables? how can I add delete table before insertion in the command line?

Thanks in advance!

4

There are 4 answers

0
CoryatJohn On

You could manually do a truncate on the table prior to importing it. I don't think there are any command line options to include a delete of the table. The other alternative might be to drop the database prior to importing the data. You'd naturally have to include the schema then.

0
Craig Ringer On

Use pg_dump -Fc to emit a custom-format dump, then restore with pg_restore --clean. This will drop the tables and re-create them, rather than deleting from them. It should be obvious, but don't do this if there's anything of value in the database you are restoring to.

0
Octo On

Ok, maybe I should have clarified more, my goal was to update the one db while keeping not updating the tables there, I thought of updating in the beginning only the tables I wanted but instead(after I encountered the problem I wrote above), I'm thinking the best solution for me was just to exclude using the -T in the console(which I missed on the documentation) , worked fine.

1
concept47 On

With newer versions of Postgres you can now specify an --exclude-table-data=tablename flag and it will exclude the table from the dump