|
| 1 | +# PostgreSQL 备份 / 恢复 Cheat Sheet |
| 2 | + |
| 3 | +## 0. 先记住 4 条 |
| 4 | + |
| 5 | +- **备份单个数据库**:`pg_dump` |
| 6 | +- **恢复 `.dump/.backup/.tar/目录`**:`pg_restore` |
| 7 | +- **恢复 `.sql`**:`psql` |
| 8 | +- **备份整个实例(所有库 + 角色/表空间)**:`pg_dumpall` |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 1. 最常用场景 |
| 13 | + |
| 14 | +### 1.1 备份单个数据库(推荐:custom 格式) |
| 15 | + |
| 16 | +```bash |
| 17 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fc -f "D:\backup\mydb.dump" |
| 18 | +``` |
| 19 | + |
| 20 | +**说明** |
| 21 | + |
| 22 | +- `-Fc` = custom 格式,最推荐 |
| 23 | +- 优点:可压缩、可选择性恢复、支持 `pg_restore` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### 1.2 恢复到一个新数据库 |
| 28 | + |
| 29 | +先建库: |
| 30 | + |
| 31 | +```bash |
| 32 | +createdb -h 127.0.0.1 -p 5432 -U postgres mydb_restore |
| 33 | +``` |
| 34 | + |
| 35 | +再恢复: |
| 36 | + |
| 37 | +```bash |
| 38 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore "D:\backup\mydb.dump" |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +### 1.3 恢复并覆盖已有对象 |
| 44 | + |
| 45 | +```bash |
| 46 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore --clean --if-exists "D:\backup\mydb.dump" |
| 47 | +``` |
| 48 | + |
| 49 | +**说明** |
| 50 | + |
| 51 | +- `--clean`:先 drop 再重建 |
| 52 | +- `--if-exists`:避免对象不存在时报错 |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## 2. SQL 文本格式 |
| 57 | + |
| 58 | +### 2.1 备份为 `.sql` |
| 59 | + |
| 60 | +```bash |
| 61 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fp -f "D:\backup\mydb.sql" |
| 62 | +``` |
| 63 | + |
| 64 | +### 2.2 恢复 `.sql` |
| 65 | + |
| 66 | +```bash |
| 67 | +createdb -h 127.0.0.1 -p 5432 -U postgres mydb_restore |
| 68 | +psql -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -v ON_ERROR_STOP=1 -f "D:\backup\mydb.sql" |
| 69 | +``` |
| 70 | + |
| 71 | +**注意** |
| 72 | + |
| 73 | +- `.sql` 用 `psql` |
| 74 | +- 不要用 `pg_restore` 恢复 `.sql` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## 3. 备份格式怎么选 |
| 79 | + |
| 80 | +| 格式 | 参数 | 特点 | 恢复工具 | |
| 81 | +|---|---|---|---| |
| 82 | +| plain SQL | `-Fp` | 可读、可编辑 | `psql` | |
| 83 | +| custom | `-Fc` | 最常用,支持选择性恢复 | `pg_restore` | |
| 84 | +| directory | `-Fd` | 支持并行备份/恢复,适合大库 | `pg_restore` | |
| 85 | +| tar | `-Ft` | 较少用 | `pg_restore` | |
| 86 | + |
| 87 | +**默认推荐** |
| 88 | + |
| 89 | +- 日常:`-Fc` |
| 90 | +- 大库 / 追求速度:`-Fd` |
| 91 | +- 想直接看 SQL:`-Fp` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## 4. 并行备份 / 恢复 |
| 96 | + |
| 97 | +### 4.1 并行备份(directory 格式) |
| 98 | + |
| 99 | +```bash |
| 100 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fd -j 4 -f "D:\backup\mydb_dir" |
| 101 | +``` |
| 102 | + |
| 103 | +### 4.2 并行恢复 |
| 104 | + |
| 105 | +```bash |
| 106 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -j 4 "D:\backup\mydb_dir" |
| 107 | +``` |
| 108 | + |
| 109 | +### 4.3 custom 格式并行恢复 |
| 110 | + |
| 111 | +```bash |
| 112 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -j 4 "D:\backup\mydb.dump" |
| 113 | +``` |
| 114 | + |
| 115 | +**注意** |
| 116 | + |
| 117 | +- 并行备份:主要用 `-Fd` |
| 118 | +- 并行恢复:`custom` 和 `directory` 都支持 |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## 5. 按范围备份 |
| 123 | + |
| 124 | +### 5.1 只备份表结构 |
| 125 | + |
| 126 | +```bash |
| 127 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -s -f "D:\backup\schema.sql" |
| 128 | +``` |
| 129 | + |
| 130 | +### 5.2 只备份数据 |
| 131 | + |
| 132 | +```bash |
| 133 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -a -f "D:\backup\data.sql" |
| 134 | +``` |
| 135 | + |
| 136 | +### 5.3 只备份某张表 |
| 137 | + |
| 138 | +```bash |
| 139 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -t public.orders -Fc -f "D:\backup\orders.dump" |
| 140 | +``` |
| 141 | + |
| 142 | +### 5.4 只备份某个 schema |
| 143 | + |
| 144 | +```bash |
| 145 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -n public -Fc -f "D:\backup\public.dump" |
| 146 | +``` |
| 147 | + |
| 148 | +### 5.5 排除某张表 |
| 149 | + |
| 150 | +```bash |
| 151 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb --exclude-table=public.audit_log -Fc -f "D:\backup\mydb_no_audit.dump" |
| 152 | +``` |
| 153 | + |
| 154 | +### 5.6 只排除某张表的数据,保留结构 |
| 155 | + |
| 156 | +```bash |
| 157 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb --exclude-table-data=public.audit_log -Fc -f "D:\backup\mydb_no_audit_data.dump" |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 6. 按范围恢复 |
| 163 | + |
| 164 | +### 6.1 只恢复表结构 |
| 165 | + |
| 166 | +```bash |
| 167 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -s "D:\backup\mydb.dump" |
| 168 | +``` |
| 169 | + |
| 170 | +### 6.2 只恢复数据 |
| 171 | + |
| 172 | +```bash |
| 173 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -a "D:\backup\mydb.dump" |
| 174 | +``` |
| 175 | + |
| 176 | +### 6.3 只恢复某张表 |
| 177 | + |
| 178 | +```bash |
| 179 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -t public.orders "D:\backup\mydb.dump" |
| 180 | +``` |
| 181 | + |
| 182 | +### 6.4 只恢复某个 schema |
| 183 | + |
| 184 | +```bash |
| 185 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -n public "D:\backup\mydb.dump" |
| 186 | +``` |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +## 7. 备份整个实例 |
| 191 | + |
| 192 | +### 7.1 备份所有数据库 + 角色 + 表空间 |
| 193 | + |
| 194 | +```bash |
| 195 | +pg_dumpall -h 127.0.0.1 -p 5432 -U postgres -f "D:\backup\cluster.sql" |
| 196 | +``` |
| 197 | + |
| 198 | +### 7.2 恢复整个实例 |
| 199 | + |
| 200 | +```bash |
| 201 | +psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -f "D:\backup\cluster.sql" |
| 202 | +``` |
| 203 | + |
| 204 | +**注意** |
| 205 | + |
| 206 | +- `pg_dumpall` 只能输出 SQL |
| 207 | +- 恢复时通常要用高权限用户 |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## 8. 只备份角色 / 表空间(globals) |
| 212 | + |
| 213 | +```bash |
| 214 | +pg_dumpall -h 127.0.0.1 -p 5432 -U postgres --globals-only -f "D:\backup\globals.sql" |
| 215 | +``` |
| 216 | + |
| 217 | +恢复: |
| 218 | + |
| 219 | +```bash |
| 220 | +psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -f "D:\backup\globals.sql" |
| 221 | +``` |
| 222 | + |
| 223 | +**常见迁移顺序** |
| 224 | + |
| 225 | +1. 先恢复 `globals.sql` |
| 226 | +2. 再恢复各数据库 dump |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## 9. 自动建库恢复 |
| 231 | + |
| 232 | +如果想让恢复时自动创建数据库: |
| 233 | + |
| 234 | +```bash |
| 235 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -C -d postgres "D:\backup\mydb.dump" |
| 236 | +``` |
| 237 | + |
| 238 | +**说明** |
| 239 | + |
| 240 | +- `-C` = create database |
| 241 | +- 连接到一个已存在的库,一般用 `postgres` |
| 242 | + |
| 243 | +--- |
| 244 | + |
| 245 | +## 10. 常用参数速查 |
| 246 | + |
| 247 | +| 参数 | 作用 | |
| 248 | +|---|---| |
| 249 | +| `-h` | 主机 | |
| 250 | +| `-p` | 端口 | |
| 251 | +| `-U` | 用户 | |
| 252 | +| `-d` | 数据库 | |
| 253 | +| `-f` | 输出文件 | |
| 254 | +| `-Fc` | custom 格式 | |
| 255 | +| `-Fd` | directory 格式 | |
| 256 | +| `-Fp` | plain SQL 格式 | |
| 257 | +| `-j 4` | 4 并行任务 | |
| 258 | +| `-s` | schema only | |
| 259 | +| `-a` | data only | |
| 260 | +| `-t table` | 指定表 | |
| 261 | +| `-n schema` | 指定 schema | |
| 262 | +| `--clean` | 恢复前删除对象 | |
| 263 | +| `--if-exists` | 配合 `--clean` 使用 | |
| 264 | +| `-v` | verbose | |
| 265 | +| `-W` | 强制提示输入密码 | |
| 266 | +| `-O` / `--no-owner` | 不恢复对象 owner | |
| 267 | +| `-x` / `--no-privileges` | 不恢复 GRANT/ACL | |
| 268 | + |
| 269 | +--- |
| 270 | + |
| 271 | +## 11. 常见问题 |
| 272 | + |
| 273 | +### Q1. `pg_restore: input file appears to be a text format dump` |
| 274 | + |
| 275 | +原因:你拿 `.sql` 给了 `pg_restore`。 |
| 276 | +做法:改用 `psql`。 |
| 277 | + |
| 278 | +--- |
| 279 | + |
| 280 | +### Q2. 恢复时报 `role "xxx" does not exist` |
| 281 | + |
| 282 | +做法: |
| 283 | + |
| 284 | +- 先恢复 `globals.sql` |
| 285 | +- 或恢复时加: |
| 286 | + |
| 287 | +```bash |
| 288 | +pg_restore --no-owner --no-privileges -d mydb_restore "D:\backup\mydb.dump" |
| 289 | +``` |
| 290 | + |
| 291 | +--- |
| 292 | + |
| 293 | +### Q3. 恢复到新环境经常权限/owner 出错 |
| 294 | + |
| 295 | +推荐: |
| 296 | + |
| 297 | +```bash |
| 298 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore --no-owner --no-privileges "D:\backup\mydb.dump" |
| 299 | +``` |
| 300 | + |
| 301 | +--- |
| 302 | + |
| 303 | +### Q4. 恢复慢 |
| 304 | + |
| 305 | +可尝试: |
| 306 | + |
| 307 | +- 使用 `-j 4` 并行恢复 |
| 308 | +- 用 `custom` 或 `directory` 格式 |
| 309 | +- 恢复到空库 |
| 310 | +- 减少索引/约束冲突场景 |
| 311 | + |
| 312 | +--- |
| 313 | + |
| 314 | +## 12. 实战推荐套路 |
| 315 | + |
| 316 | +### 场景 A:日常单库备份 |
| 317 | + |
| 318 | +```bash |
| 319 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fc -f "D:\backup\mydb.dump" |
| 320 | +``` |
| 321 | + |
| 322 | +### 场景 B:迁移到新环境 |
| 323 | + |
| 324 | +```bash |
| 325 | +pg_dumpall -h 127.0.0.1 -p 5432 -U postgres --globals-only -f "D:\backup\globals.sql" |
| 326 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fc -f "D:\backup\mydb.dump" |
| 327 | + |
| 328 | +psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -f "D:\backup\globals.sql" |
| 329 | +createdb -h 127.0.0.1 -p 5432 -U postgres mydb |
| 330 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb --no-owner --no-privileges "D:\backup\mydb.dump" |
| 331 | +``` |
| 332 | + |
| 333 | +### 场景 C:想只恢复一张表 |
| 334 | + |
| 335 | +```bash |
| 336 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore -t public.orders "D:\backup\mydb.dump" |
| 337 | +``` |
| 338 | + |
| 339 | +--- |
| 340 | + |
| 341 | +## 13. 查看备份里有什么 |
| 342 | + |
| 343 | +```bash |
| 344 | +pg_restore -l "D:\backup\mydb.dump" |
| 345 | +``` |
| 346 | + |
| 347 | +这个命令很实用,能先看 dump 里包含哪些对象。 |
| 348 | + |
| 349 | +--- |
| 350 | + |
| 351 | +## 14. 进阶:物理备份(整库灾备) |
| 352 | + |
| 353 | +如果你需要的是 **整实例级别灾备 / PITR**,常用的是 `pg_basebackup`: |
| 354 | + |
| 355 | +```bash |
| 356 | +pg_basebackup -h 127.0.0.1 -p 5432 -U replicator -D "D:\backup\base" -Fp -Xs -P |
| 357 | +``` |
| 358 | + |
| 359 | +**适用** |
| 360 | + |
| 361 | +- 整库灾备 |
| 362 | +- 主从搭建 |
| 363 | +- 时间点恢复(配合 WAL 归档) |
| 364 | + |
| 365 | +**不适合** |
| 366 | + |
| 367 | +- 只恢复某张表 |
| 368 | +- 灵活的数据迁移 |
| 369 | + |
| 370 | +--- |
| 371 | + |
| 372 | +## 15. 最后给你一个“最短版” |
| 373 | + |
| 374 | +### 备份 |
| 375 | + |
| 376 | +```bash |
| 377 | +pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydb -Fc -f "D:\backup\mydb.dump" |
| 378 | +``` |
| 379 | + |
| 380 | +### 恢复 |
| 381 | + |
| 382 | +```bash |
| 383 | +createdb -h 127.0.0.1 -p 5432 -U postgres mydb_restore |
| 384 | +pg_restore -h 127.0.0.1 -p 5432 -U postgres -d mydb_restore "D:\backup\mydb.dump" |
| 385 | +``` |
| 386 | + |
| 387 | +### 规则 |
| 388 | + |
| 389 | +- `.sql` → `psql` |
| 390 | +- `.dump` → `pg_restore` |
| 391 | + |
| 392 | +--- |
0 commit comments