Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

351
Views
Getting error when run DockerFile to create and restoreing dump with pg_restore

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.

  1. How can I solved the problem?
  2. Is it possible RUN pg_store command without create the docker container?

Please help. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 *.sh scripts 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 *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.

postgres init

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!