我正在尝试创建一个 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
Run Code Online (Sandbox Code Playgroud)
我的Dockerfile看起来像这样:
FROM postgres:11.5
ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
Run Code Online (Sandbox Code Playgroud)
而且,我的init.sql文件如下所示:
CREATE USER mydb WITH PASSWORD 'password';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO mydb;
Run Code Online (Sandbox Code Playgroud)
你会注意到他们都没有做任何非常复杂的事情。但是,我仍然收到permission denied错误消息。我已经连接到正在运行的容器并确认 init.sql 文件在文件系统上。知道我在这里做错了什么吗?
Adi*_*iii 13
所以从这个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"]
Run Code Online (Sandbox Code Playgroud)
更新:
似乎该文件不属于 Postgres 用户所有。
尝试设置权限
ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
RUN chown postgres:postgres /docker-entrypoint-initdb.d/init.sql
Run Code Online (Sandbox Code Playgroud)
创建一个 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
Run Code Online (Sandbox Code Playgroud)
init.sql用脚本创建一个
CREATE USER platops WITH PASSWORD 'platops';
CREATE DATABASE platopsdb;
GRANT ALL PRIVILEGES ON DATABASE platopsdb TO platops;
Run Code Online (Sandbox Code Playgroud)
运行 docker-compose up -d
| 归档时间: |
|
| 查看次数: |
7117 次 |
| 最近记录: |