0

I using docker and then install postgres . Now i want export one table from postgres and insert it into mysql. I using command step by step command look like:

On database postgres I access to docker container and running command

\copy users TO  '/Users/maclocaluser/abc.csv' WITH CSV HEADER DELIMITER ';'

But it thow me exception : /Users/maclocaluser/abc.csv: No such file or directory

But when i using tool dbever or pgadmin it export success. Not error.

And then when i want import into mysql using docker i using command :

LOAD DATA  LOCAL INFILE '/Users/maclocaluser/abc.csv'  INTO TABLE users  FIELDS TERMINATED BY ','  ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;

It same thow me exception :

File '/Users/maclocaluser/abc.csv' not found (Errcode: 2 - No such file or directory) but it exist my dictory.

How to export and import it on mac . When i using ubuntu it run success. I using tool mysql or dbever it import success but command line is not.

1 Answer 1

2

Your postgres is in a Docker Container. That is a different machine than your mac, which is the Host. So, the Docker Container does not know a /Users/maclocaluser folder.

What you should do is map a directory in the docker container to a folder on the host (your Macbook). (See Volumes on Docker docs)

Then copy the data from postgres to a CSV in that directory on the container. And the Volumes will make sure it end up on your Mac.

As an example, see the docker-compose.yml below.

version: '3.9'

services:

  db:
    image: postgres:latest
    restart: "no"
    container_name: db
    volumes:
      - ./database:/var/lib/postgresql/data
      - ./transfer:/var/transfer:rw
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER: myrootpassword
      POSTGRES_DB: mydatabase

It starts a database and maps the database itself to your host folder called ./database. And it also maps the folder ./transfer to /var/transfer on your Docker Container.

Your Copy command will look like this:

\copy users TO  '/var/transfer/abc.csv' WITH CSV HEADER DELIMITER ';'

If you execute this command on Postgres, your abc.csv file should end up in ./transfer on your macbook.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks you so much . but where to put my folder ./database . In /Users/maclocaluser right ? So i have a question : Why pgadmin and dbever export success it ?
If you have pgadmin running in docker also, it's on the same docker-network. In dbever you probably make a connection with port 5432. If you map the Docker-Container port 5432 to the Host-port 5432, then dbever can access the database on that port while it is running on the host.
Your folder ./database is on the same level as your docker-compose.yml file. Same for the ./transfer folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.