[英]run sql script in postgresql docker have included in DockerFile
I am learning to use docker, my goal is to create a postgres sql container in which i create a table, as of now i do not want to add any data just create the table. 我正在学习使用docker,我的目标是创建一个postgres sql容器,在其中创建一个表,到目前为止,我不想添加任何数据即可创建表。 i was able to create a postgres sql instance but copy/add my sql script (which creates this table) to /docker-entrypoint-initdb.d/ does not seem to work. 我能够创建一个postgres sql实例,但是将我的sql脚本(创建此表)复制/添加到/docker-entrypoint-initdb.d/似乎不起作用。 i can confirm this when i run the psql db i cannot see any table created. 我可以在运行psql db时确认这一点,但看不到任何创建的表。
My Dockerfile 我的Dockerfile
"FROM ubuntu
RUN apt-get update && apt-get install -y gnupg
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
USER postgres
RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -O docker docker
RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
EXPOSE 5432
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
copy /scripts/test.sql /docker-entrypoint-initdb.d/
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]"
My sql file 我的SQL文件
CREATE TABLE email_confirmation_code (
id integer NOT NULL,
code character varying(255) NOT NULL,
user_id integer
);
testing 测试
psql -h localhost -p 32772 -U docker --password
Password for user docker:
psql (10.6 (Ubuntu 10.6-1.pgdg16.04+1), server 9.3.17)
SSL connection (protocol: TLSv1.2, cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
docker=# select * from email_confirmation_code
docker-# ;
ERROR: relation "email_confirmation_code" does not exist
LINE 1: select * from email_confirmation_code
as you can see the table does not exist. 如您所见,该表不存在。 please point out what is the mistake here. 请指出这里的错误是什么。
清理/var/lib/postgresql/data
卷中的文件/文件夹并创建一个容器。
That's a feature of the standard postgres image ; 这是标准postgres图像的功能 ; it's not something you get if you install PostgreSQL by hand in a plain Ubuntu container. 如果您在一个普通的Ubuntu容器中手动安装PostgreSQL,那您将无法获得。
If you build a custom image for this, you can just start from that image and add your initialization file. 如果为此构建自定义图像,则可以从该图像开始并添加初始化文件。 This is a complete Dockerfile: 这是一个完整的Dockerfile:
FROM postgres:9.6
COPY ./scripts/test.sql /docker-entrypoint-initdb.d/
I might not build a custom image for this. 我可能不会为此构建自定义图像。 Instead, you can use the docker run -v
option or the Docker Compose volumes:
option to push the script into the container when it starts up (treating it as a configuration file). 相反,您可以使用docker run -v
选项或Docker Compose volume volumes:
选项将脚本启动时将脚本推送到容器中(将其作为配置文件进行处理)。
(Also remember that these init scripts only run the very first time the database container starts up; if there is already database data they will not re-run.) (还要记住,这些初始化脚本仅在数据库容器第一次启动时运行;如果已经有数据库数据,它们将不会重新运行。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.