Importing CSV to Postgres on Docker

First, start postgres in Docker, we’ll use a persistent volume just incase we want to drop and re-create the container.

## create volume
$ docker volume create pgdata

## run postgres
$ docker run --name postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e PGDATA=/var/lib/postgresql/data/pgdata  -v pgdata:/var/lib/postgresql/data/pgdata postgres:latest

Asuming our CSV looks like this:

id,name,category
1,foo,bar
2,fizz,buzz
3,foobar,bar

Run psql and create a table for our data.

$ docker exec -it postgres psql -U postgres
create table t1 (
    id int,
    name citext,
    category citext
);

And finally, import the CSV by piping to STDIN.

$ cat data.csv | docker exec -i postgres psql -U postgres -c "copy t1 from stdin with (format csv, header true);"
COPY 3

Done!

$ docker exec -i postgres psql -U postgres -c "select * from t1"
 id |  name  | category
----+--------+----------
  1 | foo    | bar
  2 | fizz   | buzz
  3 | foobar | bar
(3 rows)