1

I can't create a postgres sql database using docker-compose.yml and an init file: I'm using docker-compose to create my local database without success

What I have inside my docker-compose file is the following:


version: '3'

services: 
    postgresql:
        image: postgres:alpine
        container_name: postgresql
        networks:
            - des-erp
        env_file:
            - ./postgresql/database.env
        ports:
            - 5444:5444
        volumes:
            - ./postgresql/init.sql:/docker-entrypoint-initdb.d/init.sql
            - postgres_data:/var/lib/postgresql/data

So I'm using a init file as an entrypoint, this is what the sql file looks like:

CREATE DATABASE my_datbase;

The path is correct, and I'm using the command: "docker-compose up -d" in the folder the docker-compose.yml is placed.

It does create the container but can't run the init.sql file, the error I can see in the container once created is the following:

psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory.

My directory looks like the following, I'm in the "Backend" directory and this is what I can see:

ª   .gitlab-ci.yml
ª   docker-compose.yml
ª   README.md
+---postgresql\
        database.env
        init.sql

I do have the postgresql\init.sql file in the right place I believe
Im using Windows 11 btw
Any ideas?

2
  • Update: It does wrongly create a folder called "init.sql", that's why it can't execute it. I've added the ":ro" option to tell that this is a read-only file without success. Commented Apr 3, 2023 at 2:10
  • 2
    If Docker is creating init.sql as a directory, it means that there is no file in your local directory named ./postgresql/init.sql. Please update your question with the output of running tree . or find * -type f from the same directory that contains your docker-compose.yaml. Commented Apr 3, 2023 at 2:14

2 Answers 2

4

in volumes section you specify path to dirs not path to files

change

volumes:
   ./postgresql/init.sql:/docker-entrypoint-initdb.d/init.sql

to

volumes:
   ./postgresql:/docker-entrypoint-initdb.d

or to

volumes:
   <path to dir with init.sql>:/docker-entrypoint-initdb.d

where <path to dir with init.sql> is propper local path

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this one, It didn't work, it doesn't copy the file
-1

Looks like the following things to be double-checked.

ports:
    - 5444:5444

The default port number is 5432. Please confirm if 5444 is the correct port number.

CREATE DATABASE my_datbase;

Please confirm if the correct name for the database is my_database.

Actually, the container I created on my local machine seems to be working well after confirming the above information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.