我需要使用PostgreSQL创建一个容器,并且我有一个文件init.sql,其中包含数据库的结构,最后是一个从CSV复制数据的命令。
我的init.sql是:
CREATE SEQUENCE aditivo_id_seq;
CREATE TABLE public.aditivo
(
id BIGINT NOT NULL DEFAULT nextval ('aditivo_id_seq'::regclass),
clasificacion CHARACTER VARYING (200) NOT NULL,
descripcion CHARACTER VARYING (4000) NOT NULL,
id_aditivo CHARACTER VARYING (10) NOT NULL,
nombre CHARACTER VARYING (200) NOT NULL,
origen CHARACTER VARYING (40) NOT NULL,
peligro CHARACTER VARYING (200) NOT NULL,
CONSTRAINT aditivo_pkey PRIMARY KEY (id)
NOT DEFERRABLE INITIALLY IMMEDIATE,
CONSTRAINT uk_c1t6nik8nbqfei52k7rlo28lv UNIQUE (id_aditivo)
NOT DEFERRABLE INITIALLY IMMEDIATE
);
COPY aditivo (clasificacion, descripcion, id_aditivo, nombre, origen, peligro) FROM '/var/lib/postgresql/csvs/aditivos.csv' DELIMITER ',' CSV HEADER;
我的docker-compose.yml是:
version: "3.3"
services:
postgreSQL:
restart: on-failure
image: postgres:latest
volumes:
- ./postgres/aditivos.csv:/var/lib/postgresql/csvs
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
POSTGRES_USER: UALappDitivos
POSTGRES_PASSWORD: TFGappDitivosUAL!
POSTGRES_DB: db
ports:
- "5432:5432"
可以很好地复制init.sql,但不能复制csv。
我得到了错误:
psql:/docker-entrypoint-initdb.d/init.sql:147: ERROR: could not open file "/var/lib/postgresql/csvs/user.csv" for reading: Not a directory
我该如何正确执行此操作?
答案 0 :(得分:0)
要解决此问题,我复制文件夹而不是复制file.csv。所以我的docker-compose.yml看起来像:
version: "3.3"
services:
postgreSQL:
restart: on-failure
image: postgres:latest
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./postgres/csvs:/var/lib/postgresql/csvs
environment:
POSTGRES_USER: UALappDitivos
POSTGRES_PASSWORD: TFGappDitivosUAL!
POSTGRES_DB: db
ports:
- "5432:5432"
rest:
build: rest
restart: on-failure
depends_on:
- postgreSQL
ports:
- "8080:8080"