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?
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.
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.
) 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.
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With