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

2024/11/17 20:41:17

I would like to load data from CSV file into PostgreSQL database in Docker. I run:

docker exec -ti my project_db_1 psql -U postgres

Then I select my database:

\c myDatabase

Now I try to load data from myfile.csv which is in the main directory of the Django project into backend_data table:

\copy backend_data (t, sth1, sth2) FROM 'myfile.csv' CSV HEADER;

However I get error:

myfile.csv: No such file or directory

It seems to me that I tried every possible path and nothing works. Any ideas how can I solve it? This is my docker-compose.yml:

version: '3'services:db:image: postgresenvironment:POSTGRES_USER: myuserPOSTGRES_PASSWORD: mypassworddjango:build: .command: python3 manage.py runserver 0.0.0.0:8000volumes:- .:/codeports:- "8000:8000"depends_on:- db
Answer

The easiest way is to mount a directory into the postgres container, place the file into the mounted directory, and reference it there.

We are actually mounting the pgdata directory, to be sure that the postgres data lives even if we recreate the postgres docker container. So, my example will also use pgdata:

services:db:image: postgresenvironment:POSTGRES_USER: myuserPOSTGRES_PASSWORD: mypasswordvolumes:- "<path_to_local_pgdata>:/var/lib/postgresql/data/pgdata"

Place myfile.csv into <path_to_local_pgdata> (relative to directory containing the config or absolute path). The copy command then looks like this:

\copy backend_data (t, sth1, sth2) FROM '/var/lib/postgresql/data/pgdata/myfile.csv' CSV HEADER;
https://en.xdnf.cn/q/71150.html

Related Q&A

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…

Python Last Iteration in For Loop [duplicate]

This question already has answers here:What is the pythonic way to detect the last element in a for loop?(34 answers)How do I read and write CSV files?(7 answers)Closed 9 months ago.Is there any simp…

Django 1.7 multisite User model

I want to serve a Django application that serves multiple web sites by single database but different user sets. Think like a blog application, it will be used by several domains with different themes, …

Does for key in dict in python always iterate in a fixed order?

Does the python codefor key in dict:..., where dict is a dict data type, always iterate in a fixed order with regrard to key? For example, suppose dict={"aaa":1,"bbb",2}, will the …

Kinesis Firehose lambda transformation

I have the following lambda function as part of Kinesis firehose record transformation which transforms msgpack record from the kinesis input stream to json.Lambda Runtime: python 3.6from __future__ im…

Python: find out whether a list of integers is coherent

I am trying to find out whether a list of integers is coherent or at one stretch, meaning that the difference between two neighboring elements must be exactly one and that the numbers must be increasin…