Origin: Restore One Table with psql from a pg_dump PostgreSQL Backup
Backing up and restoring PostgreSQL is fairly easy using pg_dump andpsql. One more complicated scenario I have run into is doing a complete database backup with pg_dump and at some point down the road needing to just split out one table and restore it. This can be a pain because of how the pg_dump organizes the .sql file it outputs. I have found the best way to do this is to use pg_dump to backup one table to understand the format of what is needed for a restore. Then search the .sql file that was output from the full pg_dump output and split out the necessary data to restore into one table. Below I provide some examples as well as syntax to use for backing up and/or restoring PostgreSQL using pg_dump and psql.
First lets look at the syntax required to backup a PostgreSQL database using pg_dump which I have done below. The first command will backup the entire database and output to a SQL file in the default format.
pg_dump -U postgres dbname > data-dump.sql