Newbie
Newbie

Reputation: 21

How to \COPY a CSV into a PostgreSQL Docker Container?

I'm trying to query a CSV into a DB, but always returns the error (No such file or directory).

The file should be hosted on the /tmp folder on the container?

Database: postgres:13.2-alpine (Docker container)

Backend: node + slonik (on top of pg)

docker-compose.yml

version: '3.8'
services:
  db:
    image: postgres:13.2-alpine
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: <>
      POSTGRES_PASSWORD: <>
      POSTGRES_DB: <>

  db_admin:
    image: adminer
    ports:
      - 8080:8080
    depends_on:
      - db

Desired query:

\COPY rams(ram) FROM '/Users/…/rams.csv' WITH (FORMAT CSV) HEADER;

Error:

node:59682) UnhandledPromiseRejectionWarning: error: could not open file "/Users/…/rams.csv" for reading: No such file or directory

Best,

Upvotes: 2

Views: 2468

Answers (1)

pebwindkraft
pebwindkraft

Reputation: 153

There are several options to make connection between your local machine and a docker container, running the database. As already mentioned in my comment, looking up the section on volumes in docker documentation provides an overview. But this does not really fit, when you have an already running database container. This is what I do in such a case:

docker cp my_products.csv <database docker image>:/tmp

How do we get the "database docker image"? Simply by doing a docker ps (name is in the last column).

REFERENCE/CREDITS: https://dev.to/adron/getting-copy-for-bulk-csv-working-on-a-container-running-postgresql-3kk9

Upvotes: 5

Related Questions