I have a PostgreSQL table with existing data, and needs to import all the rows in a CSV file into that table. I am using pgadmin3's Import tool to do the import.
Question: For the newly imported rows, how do I set the values for a specific column (not found in the CSV)? I cant do UPDATE staging.tablename SET colname = 'somename'
the way I could when importing into an empty table. Or is this approach wrong?
Best Answer
I'd recommend using the
COPY
command frompsql
. You can set aDEFAULT
value for a column and omit that value from theCOPY
command, eg:Alternately, you can create a new
TEMPORARY
table in PgAdmin-III wtih just the columns in the CSV, import the CSV into it, and use SQL like this to merge it into the main table:You can use this to calculate columns based on expressions, combine and split columns, omit some rows, etc.