postgresql docker 和 docker-compose 中的多个数据库

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

Multiple databases in docker and docker-compose

postgresqldockerdocker-compose

提问by Tomislav Mikulin

I have a project consisting of two main java apps that use eight postgres databases, so is there a way in docker-compose to build eight different databases so that each one has a different owner and password? Can I even do that in docker-compose?

我有一个项目由两个主要的 Java 应用程序组成,这些应用程序使用八个 postgres 数据库,那么 docker-compose 有没有办法构建八个不同的数据库,以便每个数据库都有不同的所有者和密码?我什至可以在 docker-compose 中做到这一点吗?

example:

例子

services:
    postgresql:
        build: db/.
        ports:
            - "5432:5432"
        environment:
          - POSTGRES_DB=database1
          - POSTGRES_USER=database1
          - POSTGRES_PASSWORD=database1

I know I can put all the .sql files in the docker-entrypoint-initdb.d and postgres will make them automatically, but how do I declare what .sql file goes in what database?

我知道我可以将所有 .sql 文件放在 docker-entrypoint-initdb.d 中,postgres 会自动生成它们,但是我如何声明 .sql 文件放在哪个数据库中?

Tnx, Tom

Tnx,汤姆

采纳答案by Sergiu

According to this Github issuemight be possible to achieve multiple databases by using bash scripts which you will have to pass in your Dockerfile

根据这个 Github问题,可能可以通过使用 bash 脚本来实现多个数据库,您必须在 Dockerfile 中传递这些脚本

EDIT:

编辑

To create multiple Databases you could use the following script:

要创建多个数据库,您可以使用以下脚本:

https://github.com/mrts/docker-postgresql-multiple-databases

https://github.com/mrts/docker-postgresql-multiple-databases

or

或者

https://github.com/MartinKaburu/docker-postgresql-multiple-databases

https://github.com/MartinKaburu/docker-postgresql-multiple-databases

Which suggest that you have to clone one of the above git repos and mount it as a volume to: /docker-entrypoint-initdb.d then you would be able to pass multiple database names by using: POSTGRES_MULTIPLE_DATABASESvariable

这表明您必须克隆上述 git repos 之一并将其作为卷安装到:/docker-entrypoint-initdb.d 然后您将能够使用:POSTGRES_MULTIPLE_DATABASES变量传递多个数据库名称

回答by Przemek Nowak

Well - take a look at this Github project: https://github.com/mrts/docker-postgresql-multiple-databases

好吧 - 看看这个 Github 项目:https: //github.com/mrts/docker-postgresql-multiple-databases

According to official postgres docker image documentation:

根据官方 postgres docker 图像文档:

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files and source any *.sh scripts found in that directory to do further initialization before starting the service.

如果您想在从该镜像派生的镜像中进行额外的初始化,请在 /docker-entrypoint-initdb.d 下添加一个或多个 *.sql、*.sql.gz 或 *.sh 脚本(如有必要,创建目录)。在入口点调用 initdb 创建默认的 postgres 用户和数据库后,它将运行任何 *.sql 文件并在该目录中找到任何 *.sh 脚本以在启动服务之前进行进一步的初始化。

You will find the prepared script on that repo which you could use.

您将在该存储库中找到可以使用的准备好的脚本。