Create table in PostgreSQL docker image
In the docker-entrypoint.sh script of the official docker image of postgres is written:
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ) echo for f in /docker-entrypoint-initdb.d/*; do case "$f" in *.sh) echo "$0: running $f"; . "$f" ;; *.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;; *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;; *) echo "$0: ignoring $f" ;; esac echodone
So every .sql file you want to execute inside your docker image can just be placed inside that folder. So my dockerfile looks like
FROM postgres:9.3ENV POSTGRES_USER dockerENV POSTGRES_PASSWORD dockerENV POSTGRES_DB dockerADD CreateDB.sql /docker-entrypoint-initdb.d/
And the content of my CreateDB.sql:
CREATE TABLE web_origins ( client_id character varying(36) NOT NULL, value character varying(255));
So I just start my container with:
docker run -d my-postgres
To check:
docker exec -it 6250aee43d12 bashroot@6250aee43d12:/# psql -h localhost -p 5432 -U docker -d dockerpsql (9.3.13)Type "help" for help.docker=# \cYou 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)
You can find the details for mysql here in this blog.