![在 docker run 上创建 postgresql 表](https://linux22.com/image/185152/%E5%9C%A8%20docker%20run%20%E4%B8%8A%E5%88%9B%E5%BB%BA%20postgresql%20%E8%A1%A8.png)
我想创建一个在运行该容器时运行 PostgreSQL 数据库的 Dockerfile。
所以我所做的是制作一个 Dockerfile:
FROM postgres:latest
COPY *.sh /docker-entrypoint-initdb.d/
Dockerfile 将初始化脚本“postgres.sh”复制到 /docker-entrypoint-initdb.d/。 postgres.sh 脚本包含以下内容:
#!/bin/sh
su postgres -c "psql << EOF
ALTER USER postgres WITH PASSWORD '123';
create database shippingchallenge;
\c shippingchallenge;
create table people(firstname CHAR(20), surname CHAR(30));
insert into people values ('Pieter', 'Pauwels');
EOF"
所以我想做的是创建一个数据库“shippingchallenge”,其中包含一个表“people”,其中有两个值:“firstname”和“surname”。如果我使用sudo docker exec -it <container> /bin/bash
并粘贴它,效果很好。
但是当我使用上述方法并运行由此创建的图像时,sudo docker run -p 5432:5432 -e POSTGRES_PASSWORD=123 <image>
出现以下错误:
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 "UTF18".
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 dynamic shared memory implementation ... posix
selecting default maxconnections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: 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.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2020-12-19 12:42:09.749 UTC [45] LOG: starting PostgreSQL 13.1 (Debian 13.1-1 pgdg100+1) on x86_64 pc linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-12-19 12:42:09.754 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-12-19 12:42:09.771 UTC [46] LOG: database system was shut down at 2020-12-19 12:42:07 UTC
2020-12-19 12:42:09.778 UTC [45] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/postgres.sh
Password: su: Authentication failure
内容/etc/pam.d/su
:
auth sufficient pam_rootok.so
session required pam_env.so readenv=1
session required pam_env.so readenv=1 envfile=/etc/default/locale
session optional pam_mail.so nopen
session required pam_limits.so
@include common-auth
@include common-account
@include common-session
先谢谢您的帮助!
答案1
当构建时,将带有 SQL 语句的脚本文件放入 docker-entrypoint-initdb.d 目录即可完成此操作风俗与官方 Postgres 的图像根据图像https://github.com/docker-library/postgres
docker-context-dir
/docker-entrypoint-initdb.d
|-create-table.sql
|-Dockerfile
Dockerfile
FROM postgres:latest
COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
创建表.sql
CREATE TABLE people (
id SERIAL PRIMARY KEY,
...);
答案2
由于可执行权限以及和/bin/su
的内容没有发现问题。应该没问题吧!/etc/pam.d/su
/etc/pam.d/su-l
image
在我将图像拉到本地之后,错误似乎是由于造成的su postgres -c
;应该将其删除,因为如果您将其添加whoami
到脚本中,您会注意到该脚本将默认运行,postgresql
这是因为容器在启动时创建了一个,su
因为postgres
守护进程需要以该用户的身份启动。
Docker 文件:
FROM registry.hub.docker.com/library/postgres
COPY postgres.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/postgres.sh
脚本:
#!/bin/bash
whoami
psql << EOF
ALTER USER postgres WITH PASSWORD '123';
create database shippingchallenge;
\c shippingchallenge;
create table people(firstname CHAR(20), surname CHAR(30));
insert into people values ('Pieter', 'Pauwels');
EOF
测试:
docker exec -it <container> bash
su postgres
psql
postgres=# \l
输出(该表已创建,所有者是 postgres):
shippingchallenge|postgres|UTF8|en_US.utf8|en_US.utf8|
这就是为什么su
要求postgres
用户的密码,因为没有传递任何内容,所以你得到了验证失败在构建期间。