Monday, 4 May 2020

Dump and Restore Data from Postgres Tables

Following command can be used to dump entire tables into a .dmp file. This command does not take 'where' arguments. No filtering is done per table. The entire table will get dumped. The namespace field -n is optional. Fully qualify the table with a schema name if the table is in a particular schema other than public. eg schema.tablename. Multiple tables can be specified with multiple -t flags. 
pg_dump -h localhost -U username -d databasename -n namespace -a -t tablename1 -t tablename2 > sometables.dmp

This output file is a plain text file with a series of SQL COPY commands that can be used to recreate the table possibly in a different machine. 
To restore the file to a different machine, scp the file over to another box and use the following command to restore the tables in the file. The -f option gives the filename.
psql -h localhost -U username -d databasename -n namespace -1 -f sometables.dump

No comments:

Post a Comment