0

I have a CSV file whose data is to be imported to Postgres database , I did it using import function in pgadmin III but the problem is my CSV file changes frequently so how to import the data overwriting the already existing data in database from CSV file ?

3
  • Does the database data only reflect the CSV data? That is, if you wipe the database table containing the CSV data and reimport it, will you lose any information? Commented Sep 10, 2018 at 10:52
  • Yes, database data reflects only CSV data, if I re-import it , only the updated CSV data should be present Commented Sep 10, 2018 at 11:02
  • 2
    So just truncate the table and do the copy again? Commented Sep 10, 2018 at 12:47

1 Answer 1

1

You can save WAL logging through an optimization between TRUNCATE/COPY in the same transaction. The basic idea is to wipe the database table with TRUNCATE and reimport the data with COPY. This doesn't need to be done manually with pgAdmin each time. It can be scripted with something like:

BEGIN;
  -- The CSV file is 'mydata.csv' and the table is 'mydata'.
  TRUNCATE mydata;
  COPY mydata FROM 'mydata.csv' WITH (FORMAT csv);
COMMIT;

Note that it requires superuser access to work. The COPY command also takes various arguments, so you can adjust for different settings for null and headers etc.

Finally it should be noted that you ideally want these both to be in the same transaction. I'm not going to over-complicate this example here though as this level of care isn't needed in many of the real-world sorts of cases where one is copying in a CSV file. If you think your situation needs it, it's not too hard to track down.

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

Comments

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.