skip to Main Content

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 postgresqlinit.sql file in the right place I believe
Im using Windows 11 btw
Any ideas?

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search