1

I am trying to copy content from a csv-file to an existing but empty table in PostgreSQL. This is how far I've come:

COPY countries FROM 'C:\Program Files\PostgreSQL\9.5\data\countries-20140629.csv'
DELIMITERS ',' CSV HEADER

The Problem that I'm experiencing is that the csv-file containts three columns (code, english_name and French_name), whereas my table only persists of two columns (code, english_name). Adding a third column to my table is not an Option.

Is there any way to tell PostgreSQL to only import the first two columns of the csv-file?

1
  • Short answer: no. Possible workarounds: 1) edit the csv file, removing the second comma and everything that comes after it. 2) import into a temp table with the correct number of columns, and ignore the third column. Commented Jan 25, 2016 at 12:48

2 Answers 2

2

The easiest way is to modify your CSV and delete the last column.

You could try it like the documentation says:

In your case it would be like:

COPY countries (code, english_name) FROM 'C:\Program Files\PostgreSQL\9.5\data\countries-20140629.csv' DELIMITERS ',' CSV HEADER

Take a look in the documentation for further help: PostgreSQL.org

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

Comments

-1

As far as I can see, there is no possibility to tell postgres to only import a subset of the columns. What you can do, is to import the csv file into a temporary table and then transfer the data you want from the temporary table to your final table.

1 Comment

It is possible, see motho's answer

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.