Copy csv into container to use in init.sql - postgresql

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?

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"

Related

How can I set a path to load data from CSV file into PostgreSQL database in Docker container

I need help, Docker does not initialize .sql file from docker-entrypoint-initdb.d/
My docker-compose.yml is:
services:
postgres:
container_name: postgres
image: postgres:13
environment:
POSTGRES_USER: someUser
POSTGRES_PASSWORD: somePassword
POSTGRES_DB: someDB
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: [ "CMD", "pg_isready", "-U", "someUser" ]
interval: 5s
retries: 5
restart: always
ports:
- 5432:5432
My docker-entrypoint-initdb.d/ directory was set up manually, I copied my *.sql file into it, hoping it would be executing and populate my postgres database. It looks like this:
CREATE SCHEMA retail;
CREATE TABLE retail.user_purchase (
invoice_number varchar(10),
stock_code varchar(20),
detail varchar(1000),
quantity int,
invoice_date timestamp,
unit_price Numeric(8,3),
customer_id int,
country varchar(20)
);
COPY retail.user_purchase(invoice_number,stock_code,detail,quantity,invoice_date,unit_price,customer_id,country)
FROM '/db_data/retail.csv' DELIMITER ',' CSV HEADER;
Any help is much welcome, struggling here
you should pass the local file into the container using the volume.
services:
postgres:
container_name: postgres
image: postgres:13
environment:
POSTGRES_USER: someUser
POSTGRES_PASSWORD: somePassword
POSTGRES_DB: someDB
volumes:
- db_data/init.sql:/docker-entrypoint-initdb.d/init.sql
- db_data:/var/lib/postgresql/data
healthcheck:
test: [ "CMD", "pg_isready", "-U", "someUser" ]
interval: 5s
retries: 5
restart: always
ports:
- 5432:5432

SQL schema files not running on init in Dockererized PostgreSQL instance

I am trying to create table 'User' in my database with a docker container with PostgreSQL and pgAdmin on docker-compose up.
I am following this StackOveflow article here.
DockerFile
FROM postgres:latest
COPY schema.sql /docker-entrypoint-initdb.d/
docker-compose.yaml
version: "3.8"
services:
db:
build: .
restart: always
environment:
POSTGRES_DB: xxx
POSTGRES_USER: admin
POSTGRES_PASSWORD: xxx
PGDATA: /var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4:latest
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#xxx.com
PGADMIN_DEFAULT_PASSWORD: xxx
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
links:
- "db:pgsql-server"
volumes:
db-data:
pgadmin-data:
schema.sql
create Table Users (
id SERIAL,
uuid VARCHAR(100) NOT NULL
)
The container and admin console starts up fine. However, the table is not generated and cannot be found in pgAdmin.
I exec-ed into the container and the file schema.sql is well copied into /docker-entrypoint-initdb.d/ directory.
In the PostgreSQL Docker repository, the documentation mentions that my data directory has to be empty or the files in the init folder will not be run. Could this be my issue?
How can I create my User table in my dockerized PostgreSQL when I run the container?

PostgresDB init in docker-compose

I want init my PostgresDb during starting Docker environment.
docker-compose.yml :
version: '3.1'
services:
app:
container_name: springboot-postgresql
image: sringboot-postgresql
build:
context: .
dockerfile: ./java/Dockerfile
ports:
- "8080:8080"
depends_on:
- posgresqldb
posgresqldb:
image: postgres
build:
context: .
dockerfile: ./pg/Dockerfile
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
volumes:
- ./postgres-data:/var/lib/postgresql/data
- ./pg/init.sql:/docker-entrypoint-initdb.d/init.sql
Dockerfile:
FROM postgres:12-alpine
COPY pg/init.sql /docker-entrypoint-initdb.d/
init.sql:
CREATE USER docker;
CREATE DATABASE postgres;
CREATE TABLE public.usr (
id varchar NOT NULL,
username varchar NOT NULL,
"password" varchar NOT NULL,
active bool NULL,
email varchar NULL,
authorities varchar NULL,
accountnonexpired bool NULL,
accountnonlocked bool NULL,
credentialsnonexpired bool NULL,
enabled bool NULL,
CONSTRAINT id_pkey PRIMARY KEY (id)
);
GRANT ALL PRIVILEGES ON DATABASE postgres TO docker;
INSERT INTO public.usr
(id, username, "password", active, email, authorities, accountnonexpired, accountnonlocked, credentialsnonexpired, enabled)
VALUES('1', 'User_1', '*****', false, '', '', false, false, false, false);
init.db mounts succeeds and Docker containers create successfully, but I don't have changes in DB.
Can tou tell me what should I do else. Do I need any additional commands to initialize init.sql?
Thank you.
You don't need to copy the init.sql in Dockerfile since you already mount it inside docker-compose.yml
The container only checks docker-entrypoint-initdb.d if the database is not already initialized. That means the data folder should be empty for the init script to be run.
In your case, make sure the host directory ./postgres-data is empty before you run docker-compose up. As long as data is present in this folder then postgres will use it and not try to initialize the database again.

How to create table postgresql when start by docker compose (given answer doesn't work) [duplicate]

I know this question is already asked and also answer given. But it is not working for me. I follow the same. My postgres container is running fine. I checked inside the container that /docker-entrypoint-initdb.d/init.sql exist.I used the following docker-compose.yml.
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=postgres
- POSTGRES_DB=dev
restart: unless-stopped
# APP
profile_editor2:
build:
context: .
dockerfile: ./Dockerfile
network_mode: bridge
container_name: profile_editor2
volumes:
- ./image:/app/image
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
init.sql:-
create table sometable(id int);
No table created. I can see only the database is created. How do I create a table and also if possible insert some data into the table?
You can write your queries inside init.sql, in this squence:-
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
\c test_db;
CREATE TABLE Role(
RoleID SERIAL PRIMARY KEY,
RoleName varchar(50),
);
insert into Role(RoleName)
values ('Admin'),('User');

How to create table postgresql when start by docker compose

I know this question is already asked and also answer given. But it is not working for me. I follow the same. My postgres container is running fine. I checked inside the container that /docker-entrypoint-initdb.d/init.sql exist.I used the following docker-compose.yml.
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=postgres
- POSTGRES_DB=dev
restart: unless-stopped
# APP
profile_editor2:
build:
context: .
dockerfile: ./Dockerfile
network_mode: bridge
container_name: profile_editor2
volumes:
- ./image:/app/image
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
init.sql:-
create table sometable(id int);
No table created. I can see only the database is created. How do I create a table and also if possible insert some data into the table?
You can write your queries inside init.sql, in this squence:-
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
\c test_db;
CREATE TABLE Role(
RoleID SERIAL PRIMARY KEY,
RoleName varchar(50),
);
insert into Role(RoleName)
values ('Admin'),('User');