簡體   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