繁体   English   中英

使用 docker-entrypoint-initdb.d 脚本初始化 PostgreSQL 容器

[英]Initialize PostgreSQL Container with docker-entrypoint-initdb.d script

我正在尝试创建一个 PostgreSQL 11.5 docker 容器。 这样做时,我想运行一个 SQL 脚本来创建必要的用户、表等。 但是,每当容器启动时,我都会看到以下错误:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start


WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
****************************************************
WARNING: No password has been set for the database.
         This will allow anyone with access to the
         Postgres port to access your database. In
         Docker's default configuration, this is
         effectively any other container on the same
         system.

         Use "-e POSTGRES_PASSWORD=password" to set
         it in "docker run".
****************************************************
waiting for server to start....2019-09-16 17:16:26.568 UTC [42] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-09-16 17:16:26.677 UTC [43] LOG:  database system was shut down at 2019-09-16 17:16:25 UTC
2019-09-16 17:16:26.691 UTC [42] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
/docker-entrypoint-initdb.d/init.sql: Permission denied

我的Dockerfile看起来像这样:

FROM postgres:11.5

ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]

而且,我的init.sql文件如下所示:

CREATE USER mydb WITH PASSWORD 'password';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO mydb;

你会注意到他们都没有做任何非常复杂的事情。 但是,我仍然收到permission denied错误。 我已经连接到正在运行的容器并确认 init.sql 文件在文件系统上。 知道我在这里做错了什么吗?

用数据初始化 Postgres 容器

创建一个docker-compose.yml

version: '2'
services:
  postgress-postgresql:
    image: postgres:11.3
    volumes:
    #     - ~/volumes/jhipster/postgress/postgresql/:/var/lib/postgresql/data/
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      - POSTGRES_USER=postgress
      - POSTGRES_PASSWORD=
    ports:
      - 5432:5432

用脚本创建一个init.sql

CREATE USER platops WITH PASSWORD 'platops';
CREATE DATABASE platopsdb;
GRANT ALL PRIVILEGES ON DATABASE platopsdb TO platops;

使用docker-compose up -d

所以从这个Dockerfile我假设用户是 postgress。

试试这个 Dockerfile

FROM postgres:11.5
USER postgres
RUN whoami
ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]

更新:

似乎该文件不归 Postgres 用户所有。

尝试设置权限

ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
RUN chown postgres:postgres /docker-entrypoint-initdb.d/init.sql

在我们的案例中,底层问题是 sql 脚本存储在安装了 ntfs-3g 的 ntfs 分区上,默认情况下,该分区已禁用权限功能( https://superuser.com/questions/451475/chmod-doesnt-work ) . 在普通的 ext4 分区上运行它解决了这个问题。

我遇到了同样的问题,通过 docker 卷挂载了一个 .sh 文件。 我通过ls -lah检查了权限,在我的情况下它只是-rw-r-----

使用chmod 644 filename解决了我的问题。

免责声明:我知道这不是问题的答案,但它可能会说明为什么对某些人有效而对其他人无效。

在我们的团队内部,它非常适合用户名为“admin”(字面意思)的人。 它对我和外部部署服务器都不起作用。 我们的用户名不同的地方。 使用“sudo”没有任何影响。 它没有破坏他的它没有修复我们的。

我的机器和他的都是 MacOS。 服务器是 Ubuntu。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM