如何从 Docker 容器生成 Postgresql 转储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30171063/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:58:23  来源:igfitidea点击:

How to generate a Postgresql Dump from a Docker container?

postgresqldockercontainers

提问by Cesar Augusto Nogueira

I would like to have a way to enter into the Postgresql container and get a data dump from it.

我想有一种方法可以进入 Postgresql 容器并从中获取数据转储。

回答by Cesar Augusto Nogueira

Use the following command from a UNIX terminal:

从 UNIX 终端使用以下命令:

docker exec <container_name> pg_dump <schema_name> > backup

The following command will dump only inserts from all tables:

以下命令将仅转储所有表中的插入:

docker exec <container_name> pg_dump --column-inserts --data-only  <schema_name> > inserts.sql

回答by Jekis

I have container named postgreswith mounted volume -v /backups:/backups

我有一个名为postgres 的容器,带有挂载的卷-v /backups:/backups

To backup gziped DB my_dbI use:

要备份 gziped DB my_db我使用:

docker exec postgres pg_dump -U postgres -F t my_db | gzip >/backups/my_db-$(date +%Y-%m-%d).tar.gz

Now I have

我现在有

user@my-server:/backups$ ls
my_db-2016-11-30.tar.gz

回答by jonathanjg

Although the mountpoint solution above looked promising, the following is the only solution that worked for me after multiple iterations:

尽管上面的挂载点解决方案看起来很有希望,但经过多次迭代后,以下是唯一对我有用的解决方案:

docker run -it  -e PGPASSWORD=my_password postgres:alpine  pg_dump  -h hostname -U my_user my_db > backup.sql

What was unique in my case: I have a password on the database that needs to be passed in; needed to pass in the tag (alpine); and finally the hosts version of the psql tools were different to the docker versions.

在我的情况下有什么独特之处:我在需要传入的数据库上有一个密码;需要传入标签(高山);最后,psql 工具的主机版本与 docker 版本不同。

回答by Pratik

Another workaround method is to start postgre sql with a mountpoint to the location of the dump in docker.

另一种解决方法是使用挂载点启动 postgre sql 到 docker 中转储的位置。

like docker run -v <location of the files>. Then perform a docker inspect on the docker running container

喜欢docker run -v <location of the files>。然后在运行 docker 的容器上执行 docker inspect

docker inspect <image_id>

you can find "Volumes" tag inside and a corresponding location.Go to the location and you can find all the postgresql/mysql files.It worked for me.Let us know if that worked for you also.

你可以在里面找到“Volumes”标签和相应的位置。转到该位置,你可以找到所有的 postgresql/mysql 文件。它对我有用。让我们知道这是否也对你有用。

Good luck

祝你好运

回答by learner

To run the container that has the Postgres user and password, you need to have preconfigured variables as container environment variable. For example:

要运行具有 Postgres 用户和密码的容器,您需要预先配置变量作为容器环境变量。例如:

docker run -it --rm --link <container_name>:<data_container_name> -e POSTGRES_PASSWORD=<password> postgres /usr/bin/pg_dump -h <data_container_name> -d <database_name> -U <postgres_username> > dump.sql

docker run -it --rm --link <container_name>:<data_container_name> -e POSTGRES_PASSWORD=<password> postgres /usr/bin/pg_dump -h <data_container_name> -d <database_name> -U <postgres_username> > dump.sql