postgresql 我如何在 docker 中备份数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39210274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i backup a database in docker
提问by Kannaj
i'm running my app using docker-compose with the below yml file
我正在使用 docker-compose 和以下 yml 文件运行我的应用程序
postgres:
container_name: postgres
image: postgres:${POSTGRES_VERSION}
volumes:
- postgresdata:/var/lib/postgresql/data
expose:
- "5432"
environment:
- POSTGRES_DB=42EXP
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
node:
container_name: node
links:
- postgres:postgres
depends_on:
- postgres
volumes:
postgresdata:
As you can see here ,i'm using a named volume
to manage postgres state.
正如您在此处看到的,我正在使用 anamed volume
来管理 postgres 状态。
According to the official docs, i can backup a volume like the below
根据官方文档,我可以备份如下所示的卷
docker run --rm --volumes postgresdata:/var/lib/postgresql/data -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
Some other tutorials suggested i use the pg-dump
function provided by postgres for backups.
其他一些教程建议我使用pg-dump
postgres 提供的功能进行备份。
pg_dump -Fc database_name_here > database.bak
I guess i would have to go inside the postgres container to perform this function and mount the backup directory to the host.
我想我必须进入 postgres 容器才能执行此功能并将备份目录挂载到主机。
Is one approach better/preferable than the other?
一种方法比另一种更好/更可取吗?
回答by tck
To run pg_dump you can use docker exec
command:
要运行 pg_dump,您可以使用docker exec
命令:
To backup:
备份:
docker exec -u <your_postgres_user> <postgres_container_name> pg_dump -Fc <database_name_here> > db.dump
docker exec -u <your_postgres_user> <postgres_container_name> pg_dump -Fc <database_name_here> > db.dump
To drop db (Don't do it on production, for test purpose only!!!):
删除 db(不要在生产中这样做,仅用于测试目的!!!):
docker exec -u <your_postgres_user> <postgres_container_name> psql -c 'DROP DATABASE <your_db_name>'
docker exec -u <your_postgres_user> <postgres_container_name> psql -c 'DROP DATABASE <your_db_name>'
To restore:
恢复:
docker exec -i -u <your_postgres_user> <postgres_container_name> pg_restore -C -d postgres < db.dump
docker exec -i -u <your_postgres_user> <postgres_container_name> pg_restore -C -d postgres < db.dump
Also you can use docker-compose analog of exec. In that case you can use short services name (postgres) instead of full container name (composeproject_postgres).
您也可以使用 docker-compose 模拟 exec。在这种情况下,您可以使用简短的服务名称 (postgres) 而不是完整的容器名称 (composeproject_postgres)。
回答by Ryabchenko Alexander
Since you have
既然你有
expose:
- "5432"
you can run
你可以跑
pg_dump -U <user> -h localhost -Fc <db_name> > 1.dump
pg_dump will connect to 5432 port to make dump since it is lissened by postgres in container you will dump db from container
pg_dump 将连接到 5432 端口进行转储,因为它被容器中的 postgres 许可,您将从容器中转储 db