如何在脚本中为 Docker Postgres 创建用户/数据库 [英] How to create User/Database in script for Docker Postgres

查看:102
本文介绍了如何在脚本中为 Docker Postgres 创建用户/数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过创建自定义用户来为开发 postgres 实例设置容器 &数据库.我正在使用 官方 postgres docker 镜像.在文档中,它指示您在 /docker-entrypoint-initdb.d/ 文件夹中插入一个 bash 脚本,以使用任何自定义参数设置数据库.

我的 bash 脚本:make_db.sh

su postgres -c "createuser -w -d -r -s docker"su postgres -c "createdb -O docker docker"

Dockerfile

FROM 库/postgres运行 ["mkdir", "/docker-entrypoint-initdb.d"]添加 make_db.sh/docker-entrypoint-initdb.d/

我从 docker logs -f db(db 是我的容器名称)得到的错误是:

<块引用>

createuser:无法连接到数据库 postgres:无法连接到服务器:没有那个文件或目录

/docker-entrypoint-initdb.d/ 文件夹中的命令似乎在 postgres 启动之前正在执行.我的问题是,如何使用官方 postgres 容器以编程方式设置用户/数据库?有没有办法用脚本来做到这一点?

解决方案

EDIT - 自 2015 年 7 月 23 日起

官方 postgres docker 镜像 将运行 .sql 脚本在 /docker-entrypoint-initdb.d/ 文件夹中找到.

所以你只需要创建以下 sql 脚本:

init.sql

创建用户泊坞窗;创建数据库泊坞窗;将数据库 docker 上的所有权限授予 docker;

并将其添加到您的 Dockerfile 中:

Dockerfile

FROM 库/postgres复制 init.sql/docker-entrypoint-initdb.d/

<小时>

但自 2015 年 7 月 8 日起,如果您只需要创建用户和数据库,使用 POSTGRES_USER 会更容易POSTGRES_PASSWORDPOSTGRES_DB 环境变量:

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

或使用 Dockerfile:

FROM 库/postgres环境 POSTGRES_USER 泊坞窗环境 POSTGRES_PASSWORD 泊坞窗ENV POSTGRES_DB 泊坞窗

<小时>

对于早于 2015 年 7 月 23 日的图片

来自postgres Docker 镜像的文档,据说><块引用>

[...] 它将获取在该目录 [/docker-entrypoint-initdb.d] 中找到的任何 *.sh 脚本,以在启动服务之前进行进一步的初始化

这里重要的是在启动服务之前".这意味着您的脚本 make_db.sh 将在 postgres 服务启动之前执行,因此错误消息 无法连接到数据库 postgres".

之后还有一条有用的信息:

<块引用>

如果您需要在初始化过程中执行 SQL 命令,强烈建议使用 Postgres 单用户模式.

同意这乍一看可能有点神秘.它说的是您的初始化脚本应该在执行其操作之前以单一模式启动 postgres 服务.所以你可以改变你的 make_db.ksh 脚本如下,它应该让你更接近你想要的:

注意,最近发生了变化如下提交.这将适用于最新的更改:

export PGUSER=postgrespsql <<- EOSQL创建用户泊坞窗;创建数据库泊坞窗;将数据库 docker 上的所有权限授予 docker;数据库

以前,需要使用 --single 模式:

gosu postgres postgres --single <<- EOSQL创建用户泊坞窗;创建数据库泊坞窗;将数据库 docker 上的所有权限授予 docker;数据库

I have been trying to set up a container for a development postgres instance by creating a custom user & database. I am using the official postgres docker image. In the documentation it instructs you to insert a bash script inside of the /docker-entrypoint-initdb.d/ folder to set up the database with any custom parameters.

My bash script: make_db.sh

su postgres -c "createuser -w -d -r -s docker"
su postgres -c "createdb -O docker docker"

Dockerfile

FROM library/postgres

RUN ["mkdir", "/docker-entrypoint-initdb.d"]
ADD make_db.sh /docker-entrypoint-initdb.d/

The error I get from the docker logs -f db (db is my container name) is:

createuser: could not connect to database postgres: could not connect to server: No such file or directory

It seems that the commands inside of the /docker-entrypoint-initdb.d/ folder are being executed before postgres is started. My question is, how do I set up a user/database programmatically using the official postgres container? Is there any way to do this with a script?

解决方案

EDIT - since Jul 23, 2015

The official postgres docker image will run .sql scripts found in the /docker-entrypoint-initdb.d/ folder.

So all you need is to create the following sql script:

init.sql

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

and add it in your Dockerfile:

Dockerfile

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/


But since July 8th, 2015, if all you need is to create a user and database, it is easier to just make use to the POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB environment variables:

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

or with a Dockerfile:

FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker


for images older than Jul 23, 2015

From the documentation of the postgres Docker image, it is said that

[...] it will source any *.sh script found in that directory [/docker-entrypoint-initdb.d] to do further initialization before starting the service

What's important here is "before starting the service". This means your script make_db.sh will be executed before the postgres service would be started, hence the error message "could not connect to database postgres".

After that there is another useful piece of information:

If you need to execute SQL commands as part of your initialization, the use of Postgres single user mode is highly recommended.

Agreed this can be a bit mysterious at the first look. What it says is that your initialization script should start the postgres service in single mode before doing its actions. So you could change your make_db.ksh script as follows and it should get you closer to what you want:

NOTE, this has changed recently in the following commit. This will work with the latest change:

export PGUSER=postgres
psql <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

Previously, the use of --single mode was required:

gosu postgres postgres --single <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

这篇关于如何在脚本中为 Docker Postgres 创建用户/数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆