Skip to content

Commit

Permalink
docs: add how-to-backup-postgre-docker-containe
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Jul 10, 2024
1 parent 20f09f6 commit 2678582
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: back-end/how-to-backup-postgres-docker-container
date: 2024-07-10 17:18:24
tags:
---

# 使用docker-compose备份Postgres Docker容器的解决方案

## 备份

使用`pg_dumpall`命令备份Postgres数据库。

```bash
docker-compose exec <postgres_service> pg_dumpall -U postgres > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql
```

1. `docker-compose exec <postgres_service>` 在名为`<postgres_service>`的Postgres容器上执行命令。
2. `-U postgres` 指定数据库的用户名。Docker的默认用户名是`postgres`,如果你使用不同的用户名,请进行修改。

## 恢复

`dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql`文件放置在`backup`文件夹中。

然后使用Docker卷将`backup`文件夹绑定到Postgres容器上:

```bash
volumes:
- ./backup:/backup
```

删除现有的Postgres容器并创建一个新的容器:

```bash
docker-compose down && docker-compose up -d
```

执行数据库导入命令:

```bash
docker-compose exec postgres psql -f /backup/dump_xxx.sql postgres -U postgres
```

## 参考资料

1. [使用docker-compose备份和恢复Postgres数据库](https://wdt.im/posts/docker-compose-postgres-backup-restore/)

0 comments on commit 2678582

Please sign in to comment.