masa_ekohe
masa_ekohe

Reputation: 79

How to import only specific columns from CSV in PostgreSQL

PostgreSQL version: 9.6

I have a table xyz with columns a integer, b integer, c text, d integer.

The column c has a non-null constraint.

I have a CSV looking like so:

foo.csv

1,4
2,5
3,7

How could I only update column a and d by importing the CSV above?

I tried COPY xyz (a,d) FROM '/foo.csv' DELIMITER ','; but it gave me an error

ERROR: null value in column "c" violates not-null constraint DETAIL: Failing row contains (1, null, null, 4).

Upvotes: 1

Views: 869

Answers (1)

klin
klin

Reputation: 121919

Usually, a not null column should have a default value defined. You can define a default value for an existing column, e.g.

alter table xyz alter c set default '';

After that, the copy command will succeed.

Upvotes: 1

Related Questions