Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create table and scheme postgres using docker compose

I am trying to create a postgres database with schema and table in it using docker- compose file.

I have read some posts regarding this topic and tried the suggested solutions: Copying the .sql files into the /docker-entrypoint-initdb.d/ using a volume as they should get executed.

My docker- compose is:

  postgres_db:
       image: postgres:latest
       hostname: postgres-db
       container_name: postgres
       expose:
         - "5432"
       ports:
         - "5432:5432"
       networks:
         - default
       environment:
         - "POSTGRES_PASSWORD=password"
         - "POSTGRES_USER=postgres"
         - "POSTGRES_DB=postgres"
       volumes:
         - ./scripts/postgres:/docker-entrypoint-initdb.d/

the files in /scripts/postgres are:

1-schema.sql

CREATE SCHEMA myschema;

2-table.sql

CREATE TABLE myschema.mytable(id integer PRIMARY KEY, purchase_date date, unit integer, description varchar(300));

And I am getting the following error:

postgres       | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/1-schema.sql
postgres       | psql:/docker-entrypoint-initdb.d/1-schema.sql:0: could not read from input file: Is a directory
postgres exited with code 1

Also I tried to define the volumes as:

       volumes:
         - ./scripts/postgres/1-schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
         - ./scripts/postgres/2-table.sql:/docker-entrypoint-initdb.d/2-table.sql

but still getting same error :(

Any suggestion about why is it failing and a possible fix?

EDIT: @Mihai, it doesn't seems that there is a folder named 1-schema.sql. Below the output of the proposed command:

$ ls -lart ./scripts/postgres
total 16
-rw-r--r--@ 1 user  staff   26 13 may 22:21 1-schema.sql
-rw-r--r--@ 1 user  staff  152 13 may 22:21 2-table.sql
drwxr-xr-x  4 user  staff  128 13 may 22:43 .
drwxr-xr-x  9 user  staff  288 13 may 22:50 ..

$ cat ./scripts/postgres/1-schema.sql
CREATE SCHEMA myschema;
$ cat ./scripts/postgres/2-table.sql
CREATE TABLE myschema.mytable(id integer PRIMARY KEY, purchase_date date, unit integer, description varchar(300));

Thanks in advance!

like image 450
BlairW Avatar asked Jul 31 '26 10:07

BlairW


1 Answers

I could reproduce your issue by creating and actual folder:

mkdir ./scripts/postgres/0-schema.sql
docker-compose up

Then I get exactly your error:

postgres       | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/0-schema.sql
postgres       | psql:/docker-entrypoint-initdb.d/0-schema.sql:0: could not read from input file: Is a directory

Double check your local setup because you might have a folder called 1-schema.sql in your "scripts/postgres" folder. Check this by running in your project folder:

ls -lart ./scripts/postgres
like image 135
Mihai Avatar answered Aug 03 '26 02:08

Mihai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!