My init.sql file is :
CREATE USER postgres WITH PASSWORD '123qwe';
CREATE DATABASE gmta_database ;
GRANT ALL PRIVILEGES ON DATABASE gmta_database TO postgres;
And DockerFile is:
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD 123qwe
ENV POSTGRES_DB docker_pg
EXPOSE 5432
COPY test_latest.sql /
VOLUME /var/lib/postgresql/data
RUN pg_restore -U postgres -d docker_pg < test_latest.sql
When I run with command docker build -t gmta-test-vol:1.0.0 . , I am getting error like this:
Sending build context to Docker daemon 358.4kB
Step 1/7 : FROM postgres:latest
---> b97bae343e06
Step 2/7 : COPY init.sql /docker-entrypoint-initdb.d/
---> Using cache
---> 6f275b44db01
Step 3/7 : ENV POSTGRES_USER postgres
---> Using cache
---> 039924093b36
Step 4/7 : ENV POSTGRES_PASSWORD 123qwe
---> Using cache
---> 5e636686a2f7
Step 5/7 : ENV POSTGRES_DB docker_pg
---> Using cache
---> 9c0a773c138c
Step 6/7 : COPY gtma_latest.sql /
---> Using cache
---> 8dd99f79b403
Step 7/7 : RUN pg_restore -U postgres -d docker_pg < gtma_latest.sql
---> Running in 1e0a85650eb1
pg_restore: error: connection to database "docker_pg" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I want to create the database and restore the dump data with single docker command.
Please help. Thanks in advance.
You should not import data at build time, as DB server is not ready also this will not persistent the import for the subsequent layer.
All you need to add this
COPY test_latest.sql /docker-entrypoint-initdb.d/
If you would like to do additional initialization in an image derived from this one, add one or more
*.sql, *.sql.gz, or *.shscripts under/docker-entrypoint-initdb.d(creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any*.sqlfiles, run any executable *.sh scripts, and source any non-executable*.shscripts found in that directory to do further initialization before starting the service.
How can I solved the problem?
Just place the sql file into /docker-entrypoint-initdb.d/ and Postgres container will take care of it.
Is it possible RUN pg_store command without create the docker container?
No, place copy at build, it will create a database whenever container started.