Skip to content

Instantly share code, notes, and snippets.

@andrewp-as-is
Last active July 19, 2021 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewp-as-is/ec999dabe9f1a6e4143c2ce77366bfcb to your computer and use it in GitHub Desktop.
Save andrewp-as-is/ec999dabe9f1a6e4143c2ce77366bfcb to your computer and use it in GitHub Desktop.
postgres csv import
1 new_value1
2 new_value2
CREATE TABLE IF NOT EXISTS t(
id serial PRIMARY KEY,
value text
);
CREATE TEMP TABLE csv AS TABLE t;
INSERT INTO t(id,value) VALUES (1,'value1');
COPY csv FROM :FILE DELIMITER ',' CSV;
--COPY csv FROM :FILE DELIMITER ',' CSV HEADER; --1st line is header
INSERT INTO t(id,value)
SELECT id,value FROM csv
WHERE NOT EXISTS (SELECT id FROM t WHERE id = csv.id);
UPDATE t
SET value = (SELECT csv.value FROM csv WHERE csv.id = t.id )
WHERE EXISTS (SELECT * FROM csv WHERE csv.id = t.id AND csv.value != t.value);
SELECT * FROM t;
#!/usr/bin/env bash
db="tmp_$(openssl rand -hex 6)"
createdb "$db"
psql -v "FILE='$PWD/data.csv'" -f import.sql "$db"
dropdb "$db"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment