SantiSori
SantiSori

Reputation: 411

Copy csv into container to use in init.sql

i need to create a container with PostgreSQL and i have a file init.sql which contains the structure of the DB and at the end a command to copy data from a CSV.

My init.sql is:

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;

And my docker-compose.yml is:

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"

The init.sql is copied ok, but the csv no.

I get the error:

psql:/docker-entrypoint-initdb.d/init.sql:147: ERROR: could not open file "/var/lib/postgresql/csvs/user.csv" for reading: Not a directory

How can i do this correctly?

Upvotes: 0

Views: 1614

Answers (1)

SantiSori
SantiSori

Reputation: 411

To solve this, i copy the folder instead to copy the file.csv. So my docker-compose.yml looks like:

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"

Upvotes: 4

Related Questions