在 PostgreSQL docker 镜像中创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38713597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create table in PostgreSQL docker image
提问by Vaibhav Kumar
I am new to docker. I have extended PostgreSQL image and able to create new DB instance while running the image.I need to create tables in that DB from shell script that will execute .sql file. While running PostgreSQL image it says server not started ? how we can execute script only after the PostgreSQL server starts in docker container?
我是码头工人的新手。我已经扩展了 PostgreSQL 映像,并且能够在运行该映像时创建新的数据库实例。我需要从将执行 .sql 文件的 shell 脚本在该数据库中创建表。运行 PostgreSQL 映像时,它说服务器未启动?如何仅在 docker 容器中启动 PostgreSQL 服务器后才能执行脚本?
Dockerfile:
Dockerfile:
FROM postgres:9.3
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker
RUN apt-get update && apt-get install -y postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
ADD initialize.sh /home/MM0312/docker-entrypoint-initdb.d/initialize.sh
CMD ["/home/MM0312/docker-entrypoint-initdb.d/initialize.sh"]
initialize.sh
初始化.sh
#!/bin/bash
set -e
set -x
echo "******PostgreSQL initialisation******"
gosu docker psql -h localhost -p 5432 -U docker -d $docker -a -f createDatabase.sql
createDatabase.sql file
创建Database.sql 文件
CREATE TABLE web_origins (
client_id character varying(36) NOT NULL,
value character varying(255)
);
回答by lvthillo
In the docker-entrypoint.sh script of the official docker image of postgresis written:
postgres官方docker镜像的docker-entrypoint.sh脚本中写着:
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "FROM postgres:9.3
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker
ADD CreateDB.sql /docker-entrypoint-initdb.d/
: running $f"; . "$f" ;;
*.sql) echo "CREATE TABLE web_origins (
client_id character varying(36) NOT NULL,
value character varying(255)
);
: running $f"; "${psql[@]}" < "$f"; echo ;;
*.sql.gz) echo "docker run -d my-postgres
: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
*) echo "docker exec -it 6250aee43d12 bash
root@6250aee43d12:/# psql -h localhost -p 5432 -U docker -d docker
psql (9.3.13)
Type "help" for help.
docker=# \c
You are now connected to database "docker" as user "docker".
docker=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+--------
public | web_origins | table | docker
(1 row)
: ignoring $f" ;;
esac
echo
done
So every .sql file you want to execute inside your docker image can just be placed inside that folder. So my dockerfile looks like
因此,您要在 docker 映像中执行的每个 .sql 文件都可以放在该文件夹中。所以我的 dockerfile 看起来像
##代码##And the content of my CreateDB.sql:
和我的 CreateDB.sql 的内容:
##代码##So I just start my container with:
所以我只是开始我的容器:
##代码##To check:
去检查:
##代码##You can find the details for mysql here in this blog.
您可以在此博客中找到有关 mysql 的详细信息。