How to restore a single table from a .sql postgresql backup?

前端 未结 7 607
耶瑟儿~
耶瑟儿~ 2020-12-09 04:19

A table\'s rows were mistakenly deleted from the database. We have a db backup which results in a sql file that can restored like so:

psql -h localhost -d pr         


        
7条回答
  •  时光取名叫无心
    2020-12-09 04:29

    I've recently written a step by step guide on how to restore individual postgres tables.

    In short it doesn't work with SQL backups, you need to switch to pg_dump to generate backup files:

    pg_dump.exe --host localhost --port 5432 --username "postgres" --schema "public" --format custom --blobs --verbose --file "my_database.dump" "my_database"
    

    Then, whenever you need to restore a specific table for data recovery or bug investigating purposes, you'll have to:

    1. Create an empty local database my_database_restored
    2. Create the table that needs to be restored my_table in the empty database
    3. Run pg_restore to selectively import desired table’s data:
    pg_restore.exe -U postgres --data-only -d "my_database_restored" -t "my_table" "my_database.dump"
    

提交回复
热议问题