PostgreSQL – Keep Empty String Instead of Null for /copy Command

bulkcopypostgresql

When using the \copy command with psql I have a file with empty values. These values are not to be null but instead always an empty string.

My command:

\copy agltransact FROM 'dbo_agltransact.csv' with null as '' CSV HEADER;

The error I get:

ERROR:  null value in column "apar_id" violates not-null constraint

I have this data:

account,amount,apar_id,apar_type
14015,3000000.000,,1
14015,3000000.000,,2

I want the next to last columns to be an empty string.

Best Answer

Figured it out. I need to escape and output the single quotes

\copy agltransact FROM 'dbo_agltransact.csv' with null as E'\'\'' CSV HEADER;