Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres CSV COPY from/import is not respecting CSV headers

Tags:

csv

postgresql

I'm trying to import data from CSV into the table. The issue is that even with CSV HEADER, the CSV is being imported based on the column index, not on the headers of that column.

CREATE TABLE denominations (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

CREATE TABLE churches (
  id SERIAL PRIMARY KEY,
  -- NOT relevant here
  address_id INTEGER REFERENCES addresses,
  denomination_id INTEGER NOT NULL REFERENCES denominations,
  name VARCHAR(100) NOT NULL
);

My CSVs look like:

id,name
1,Southern Baptist Convention
2,Nondenominational
3,Catholic
4,Presbyterian


id,denomination_id,name,address_id
1,1,Saddleback Church,
2,4,First Presbyterian Church,
3,3,St. Elizabeth's Church,
4,3,St Monica Catholic Community,
5,2,Modern Day Saints Church,
6,4,Second Presbyterian Church,

My COPY command looks like this in bash:

psql -d vacation -c "COPY denominations FROM '$PWD/data/Data - Denominations.csv' WITH DELIMITER ',' CSV HEADER;"
psql -d vacation -c "COPY churches FROM '$PWD/data/Data - Churches.csv' WITH DELIMITER ',' CSV HEADER;"

The error I get is:

ERROR:  invalid input syntax for integer: "Saddleback Church"
CONTEXT:  COPY churches, line 2, column denomination_id: "Saddleback Church"

For now, I'm going to rearrange the columns in the CSV, but shouldn't this work?

like image 290
Jonathan Ong Avatar asked Oct 22 '15 00:10

Jonathan Ong


People also ask

How to copy and import data from CSV to PostgreSQL?

We can make use of the PostgreSQL copy command to copy from the CSV file and import it to Postgres. We can import the data into the table with or without headers provided the CSV should be in the same format.

How to check if CSV file contains header or not?

Check the data have header or not Some CSV file contains the header or some file is not contains the header. The header is nothing but the column name which was we have defined in table. The header is defined in the first line of the CSV file. If the CSV file contains the header then we need to define the header in copy command.

What is the difference between copy and copy from in PostgreSQL?

) ENCODING ' encoding_name ' COPY moves data between PostgreSQL tables and standard file-system files. COPY TO copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already). COPY TO can also copy the results of a SELECT query.

How to import data from CSV file in table?

1 Create table with same structure of CSV file To import the data from CSV file into the table, same table is created with same name and same structure. ... 2 Determine the delimiter Basically we have separate the values using a comma, but in some file, we have used another delimiter to separate the values. ... 3 Check the data have header or not


1 Answers

The COPY command by default copies columns from a CSV file in the default order of the columns in the table. The HEADER option on input is ignored, it basically only informs the backend to ignore the first line on input. If the order of the columns in the CSV does not match the order of the columns in the table, you can explicitly specify the column order to match the layout of the CSV file:

COPY churches (id,denomination_id,name,address_id)
FROM '$PWD/data/Data - Churches.csv'
WITH DELIMITER ',' CSV HEADER;
like image 117
Patrick Avatar answered Sep 22 '22 20:09

Patrick