4

I have an unconventional question. I have established a db in Postgres

psql -h 192.168.99.100 -p 15432 -U postgres

And created a table in the db using:

$ docker=# CREATE TABLE cities (
docker(#     name            varchar(80),
docker(#     location        point
docker(# );

However, I haven't been able to upload a csv into the table I've created. Can you please show me how to do it? (I am using Docker Command window to do this) Thanks in advance.

3
  • where is your csv file located? can you access it from within the container? Commented Feb 7, 2018 at 18:37
  • my csv file is located on my desktop Commented Feb 7, 2018 at 18:41
  • so, I am assuming you can't access it from your container. If that's the case, you'll first have to add data volume or manually copy that file when you build the image. check stackoverflow.com/questions/46849539/… to get started Commented Feb 7, 2018 at 18:48

1 Answer 1

3

Here's a sample of copying lat/lon points in a CSV to the cities table using psql inside of a container.

# Sample CSV data
echo "somecity",45,-45 > cities.csv

# Create database
docker run --name postgres -p 15432:5432 -d postgres

# Create cities table and a temp table for loading point coordinates
# Uses host network to access database published on port 15432 of the host
docker run --rm --network=host postgres psql -h localhost -p 15432 -U postgres -c '
  CREATE TABLE temp (
    name varchar(80),
    latitude numeric(12,8),
    longitude numeric(12,8)
  );
  CREATE TABLE cities (
    name varchar(80),
    location point
  );'

# \copy data from file (mounted by volume)
docker run --rm --network=host -v `pwd`:/data postgres \
  psql -h localhost -p 15432 -U postgres -c \
    "\\copy temp FROM '/data/cities.csv' WITH csv"

# Insert into cities creating point from coordinates
docker run --rm --network=host postgres psql -h localhost -p 15432 -U postgres -c "
  INSERT INTO cities (name, location)
  SELECT name, point(temp.latitude,temp.longitude) 
  FROM temp;
  DROP TABLE temp"

# Show the point
docker run --rm --network=host postgres psql -h localhost -p 15432 -U postgres -c \
  "SELECT * FROM cities;"

The last command outputs:

  name   | location
----------+----------
 somecity | (45,-45)
(1 row)
Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your answer. I'm really new to this thing. I'm trying your method will get back to you asap
Hi, it is giving the following error "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond." @logan rakai
The commands I gave run the database and client on the same machine so I could verify that it works locally. You may need to modify localhost to 192.168.99.100 as you wrote in your question.
sorry I'm getting bunch of errors when running the code to create a db I receive ":\Program Files\Docker Toolbox\docker.exe: error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.33/containers/create?name=postgres: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running."
These errors appear to be related to your installation of Docker and not related to my answer or the initial question. I'd suggest checking your docker installation and posting a new question if necessary.
|

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.