我正在尝试使用 docker-compose 文件创建一个包含架构和表的 postgres 数据库。
我已经阅读了有关此主题的一些帖子,并尝试了建议的解决方案:使用卷将 .sql 文件复制到 /docker-entrypoint-initdb.d/ 中,因为它们应该被执行。
我的 docker-compose 是:
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/
Run Code Online (Sandbox Code Playgroud)
/scripts/postgres 中的文件是:
1-schema.sql
CREATE SCHEMA myschema;
Run Code Online (Sandbox Code Playgroud)
2-table.sql
CREATE TABLE myschema.mytable(id integer PRIMARY KEY, purchase_date date, unit integer, description varchar(300));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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 …Run Code Online (Sandbox Code Playgroud)