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

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

问题描述

我一直在尝试通过创建一个自定义用户&数据库。我正在使用官方postgre码头图片。在文档中,它指示您在 /docker-entrypoint-initdb.d / 文件夹内插入一个bash脚本,以使用任何自定义参数设置数据库。

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.

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



Dockerfile



Dockerfile

FROM library/postgres

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

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

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


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

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

在postgres启动之前,似乎执行 /docker-entrypoint-initdb.d / 文件夹内的命令。我的问题是,如何使用官方postgres容器设置用户/数据库?有没有办法使用脚本?

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?

推荐答案

编辑 - 自2015年7月23日起



官方postgre停泊港图片将运行 .sql /docker-entrypoint-initdb.d / 文件夹中找到的脚本。

EDIT - since Jul 23, 2015

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

所有您需要的是创建以下sql脚本:

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;

并将其添加到Dockerfile中:

and add it in you Dockerfile:

Dockerfile

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






但自2015年7月8日起,如果您需要创建一个用户和数据库,那么只需使用 POSTGRES_USER POSTGRES_PASSWORD POSTGRES_DB 环境变量:


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

或一个Docker文件:

or with a Dockerfile:

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






2015



Postgres Docker图像的文档,据说,


[...]它将源代码任何* .sh脚本在该目录中找到[ /docker-entrypoint-initdb.d ]在开始服务之前进行进一步的初始化

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

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

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:


如果需要执行SQL命令部分初始化,强烈建议使用Postgres单用户模式。

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

同意这可能有点神秘的第一眼。它说的是,您的初始化脚本应该在执行其操作之前以单一模式启动Postgres服务。因此,您可以按照以下方式更改您的 make_db.ksh 脚本,它应该让您更接近您想要的:

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天全站免登陆