Last active
July 19, 2021 08:10
-
-
Save andrewp-as-is/ec999dabe9f1a6e4143c2ce77366bfcb to your computer and use it in GitHub Desktop.
postgres csv import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | new_value1 | |
---|---|---|
2 | new_value2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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