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

时间:2019-09-16 17:21:04

标签: postgresql docker

我正在尝试创建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文件在文件系统上就位。知道我在这里可能做错了什么吗?

5 个答案:

答案 0 :(得分:2)

因此,从这个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

答案 1 :(得分:1)

使用数据初始化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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

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

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

相关问题