You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

紧急求助:将PostgreSQL备份数据恢复至现有生产数据库

PostgreSQL误删数据恢复:从备份文件找回缺失记录的实操方案

Hey there, let’s tackle this PostgreSQL data recovery issue step by step— I’ve been in your shoes before, so I know how stressful this can be. Let’s start by diagnosing why your imports are failing, then walk through actionable fixes to get those missing records back.

第一步:先在开发环境验证备份文件的有效性

First things first— we need to make sure your backup file is intact and contains the data you need. Skip trying to import directly to production right now; let’s use a clean dev environment to test:

  • Create a brand-new empty database in your dev environment to avoid conflicts:
    createdb -U your_dev_user temp_recovery_db
    
  • Import your .sql backup into this temp database:
    psql -U your_dev_user -d temp_recovery_db -f your_backup_file.sql
    
  • If this throws errors, read the error message carefully— common issues here include truncated backup files, invalid SQL syntax, or missing extensions that were present in the original production database. Fix those first before moving on.
  • Once the import succeeds, verify that the missing records are present in the temp database (run a SELECT * FROM your_table WHERE ... query to confirm).

第二步:只提取你需要恢复的特定记录(避免全量覆盖)

You don’t want to overwrite existing production data, so we’ll extract only the rows that were accidentally deleted:

选项1:用pg_dump导出特定行

If you know the criteria for the missing records (e.g., a date range, specific IDs), use pg_dump to export just those rows:

pg_dump -U your_dev_user -d temp_recovery_db -t your_target_table --data-only --where "your_condition_here" > missing_records.sql

For example, if you deleted all users created after 2024-01-01:

pg_dump -U your_dev_user -d temp_recovery_db -t users --data-only --where "created_at > '2024-01-01'" > missing_users.sql

选项2:用COPY导出为CSV(更灵活处理冲突)

If you need to handle potential primary key conflicts, exporting to CSV might be easier:

-- Run this in the temp_recovery_db
COPY (SELECT * FROM your_target_table WHERE your_condition_here) TO '/path/to/missing_records.csv' WITH CSV HEADER;

第三步:安全导入到生产环境

Now let’s get those records back into production, avoiding conflicts and errors:

处理主键/约束冲突

Before importing, check which records are actually missing in production to avoid duplicate key errors:

-- Run this in production (you can connect to prod and run a cross-database query if your dev DB is accessible, or export the missing IDs first)
SELECT id FROM temp_recovery_db.your_target_table WHERE id NOT IN (SELECT id FROM prod_db.your_target_table);

Update your export command to only include these missing IDs, then import:

  • For .sql files:
    psql -U prod_user -d prod_db -f missing_records.sql
    
  • For CSV files:
    -- Run in production
    COPY your_target_table FROM '/path/to/missing_records.csv' WITH CSV HEADER;
    

If you still get conflicts, use INSERT ... ON CONFLICT to handle them gracefully:

INSERT INTO your_target_table (col1, col2, col3)
SELECT col1, col2, col3 FROM temp_recovery_db.your_target_table WHERE your_condition_here
ON CONFLICT (id) DO NOTHING; -- Or DO UPDATE if you need to overwrite existing data (use carefully!)

常见导入报错的排查

If you’re still seeing errors during import, here are the most likely culprits:

  • Permission issues: Ensure your production database user has INSERT (and TRUNCATE if needed) permissions on the target table.
  • Table structure mismatch: If production’s table schema changed since the backup (e.g., new columns, modified constraints), adjust your exported data to match. For example, add default values for new columns in your SELECT query.
  • Corrupted backup: If your .sql file is truncated, try re-generating the backup from your original source (if possible) or use pg_restore if you have a .dump file instead:
    pg_restore -U your_dev_user -d temp_recovery_db your_backup.dump
    

预防下次踩坑

To avoid this headache in the future:

  • Always run destructive commands (DELETE/UPDATE) inside a transaction: BEGIN; → run your DELETE with a SELECT first to verify → COMMIT; only if it’s correct, otherwise ROLLBACK;.
  • Take a table-level backup before making changes: pg_dump -t your_table > pre_change_backup.sql.
  • Restrict write/delete permissions in production to only trusted users, and require peer review for high-risk operations.

内容的提问来源于stack exchange,提问作者Bobort

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:07:12